static DBusHandlerResult dbusrecv_message_func(DBusConnection *connection, DBusMessage *message, void *user_data) { dbusrecv_hdl_st *p_dbusrecv_hdl = (dbusrecv_hdl_st *)user_data; if (NULL == p_dbusrecv_hdl) { printf("dbusrecv_message_func: invalid handle!\n"); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } if (dbus_message_is_method_call (message, DBUS_PMD_INTERFACE, "Echo")) { return handle_echo(connection, message, user_data); } else if (dbus_message_is_method_call (message, DBUS_PMD_INTERFACE, "Exit")) { _dbus_loop_quit (p_dbusrecv_hdl->dbusloop); p_dbusrecv_hdl->dbusloop = NULL; return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(message, DBUS_PMD_INTERFACE, "system")) { return handle_system(connection, message, user_data); } else return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; }
static DBusHandlerResult path_message_func (DBusConnection *connection, DBusMessage *message, void *user_data) { if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "Echo")) return handle_echo (connection, message); else if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "Exit")) { quit (); return DBUS_HANDLER_RESULT_HANDLED; } else return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; }
/** * \brief Blocking read from a terminal. * * \param client Terminal client state. * \param data Buffer to hold read characters. * \param length The number of characters to read. * \param read Number of characters read. This might be less than length if * line_mode is enabled and the end of line was reached or if an * error occurred. * * \return SYS_ERR_OK if successful. * TERM_ERR_IO if an I/O error occurred. * * Dispatches the read if no data is available. */ errval_t term_client_blocking_read(struct term_client *client, char *data, size_t length, size_t *read) { errval_t err; bool eol_reached = false; assert(data != NULL); assert(length > 0); assert(read != NULL); /* * Copy as many characters to the user buffer as he requested but stop if * line mode is enabled and the end of line is reached. */ while ((*read < length) && !(client->line_mode && eol_reached)) { if (client->readbuf == NULL) { /* * Dispatch events on the incoming interface until characters * arrive. */ while (client->readbuf == NULL) { err = event_dispatch(client->read_ws); if (err_is_fail(err)) { return err_push(err, TERM_ERR_IO); } } /* handle echo */ if (client->echo) { err = handle_echo(client, client->readbuf, client->readbuf_len); if (err_is_fail(err)) { return err_push(err, TERM_ERR_IO); } } /* handle triggers */ handle_triggers(client, client->readbuf, client->readbuf_len); /* filter input */ term_filter_apply(client->input_filters, &client->readbuf, &client->readbuf_len); } /* copy data to user supplied buffer */ char *end = client->readbuf + client->readbuf_len; while ((client->readbuf_pos < end) && (*read < length) && !(client->line_mode && eol_reached)) { data[(*read)++] = *client->readbuf_pos; if (client->line_mode && (*client->readbuf_pos == TERM_CLIENT_EOL_CHAR)) { eol_reached = true; } client->readbuf_pos++; } /* free readbuf */ if (client->readbuf_pos == end) { free(client->readbuf); client->readbuf = NULL; client->readbuf_pos = NULL; client->readbuf_len = 0; } } return SYS_ERR_OK; }
void handle_stlv_packet(unsigned char* packet) { stlv_packet pack = packet; char type_buf[MAX_ELEMENT_TYPE_BUFSIZE]; element_handle handle = get_first_element(pack); while (IS_VALID_STLV_HANDLE(handle)) { int type_len = get_element_type(pack, handle, type_buf, sizeof(type_buf)); log_info("Read Element: %x\n", type_buf[0]); switch (type_buf[0]) { case ELEMENT_TYPE_ECHO: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); unsigned char* data = get_element_data_buffer(pack, handle, type_buf, type_len); log_info("echo: "); print_stlv_string(data, data_len); log_info("\n"); handle_echo(data, data_len); } break; case ELEMENT_TYPE_CLOCK: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); unsigned char* data = get_element_data_buffer(pack, handle, type_buf, type_len); log_info("clock: %d/%d/%d %d:%d:%d\n", (int)data[0], (int)data[1], (int)data[2], (int)data[3], (int)data[4], (int)data[5]); handle_clock(data[0], data[1], data[2], data[3], data[4], data[5]); if (data_len >= 8) handle_phone_info(data[6], data[7]); } break; case ELEMENT_TYPE_MESSAGE: if (type_len == 2) { switch (type_buf[1]) { case ELEMENT_TYPE_MESSAGE_SMS: log_info("notification(SMS):\n"); break; case ELEMENT_TYPE_MESSAGE_FB: log_info("notification(Facebook):\n"); break; case ELEMENT_TYPE_MESSAGE_TW: log_info("notification(Twitter):\n"); break; default: break; } handle_msg_element(type_buf[1], pack, handle); } break; case ELEMENT_TYPE_FILE: handle_file(pack, handle); break; case ELEMENT_TYPE_GET_FILE: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); uint8_t* data = get_element_data_buffer(pack, handle, type_buf, type_len); STLV_BUF_BEGIN_TEMP_STRING(data, data_len); handle_get_file((char*)data); STLV_BUF_END_TEMP_STRING(data, data_len); } break; case ELEMENT_TYPE_ACTIVITY_DATA: handle_get_activity(); break; case ELEMENT_TYPE_LIST_FILES: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); uint8_t* data = get_element_data_buffer(pack, handle, type_buf, type_len); STLV_BUF_BEGIN_TEMP_STRING(data, data_len); handle_list_file((char*)data); STLV_BUF_END_TEMP_STRING(data, data_len); } break; case ELEMENT_TYPE_REMOVE_FILE: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); uint8_t* data = get_element_data_buffer(pack, handle, type_buf, type_len); STLV_BUF_BEGIN_TEMP_STRING(data, data_len); uint8_t file_name_pos = 0; for (uint8_t i = 0; i < data_len; ++i) { if (data[i] == ';') data[i] = '\0'; if (data[i] == '\0') { handle_remove_file((char*)(&data[file_name_pos])); file_name_pos = i + 1; } } STLV_BUF_END_TEMP_STRING(data, data_len); } break; #if 0 case ELEMENT_TYPE_SPORT_HEARTBEAT: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); uint8_t* data = get_element_data_buffer(pack, handle, type_buf, type_len); STLV_BUF_BEGIN_TEMP_STRING(data, data_len); handle_sports_heartbeat((char*)data); STLV_BUF_END_TEMP_STRING(data, data_len); } break; case ELEMENT_TYPE_SPORTS_DATA: handle_get_sports_data(); break; #endif case ELEMENT_TYPE_SPORTS_GRID: log_info("Get Sports Grid Request\n"); handle_get_sports_grid(); break; case ELEMENT_TYPE_ALARM: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); uint8_t* data = get_element_data_buffer(pack, handle, type_buf, type_len); if (data_len != sizeof(alarm_conf_t)) { log_info("Alarm element decode failed: length mismatch (%d/%d)", data_len, sizeof(alarm_conf_t)); } else { handle_alarm((alarm_conf_t*)data); } } break; case ELEMENT_TYPE_SN: handle_get_device_id(); break; case ELEMENT_TYPE_ACTIVITY: handle_gps_data(pack, handle); break; case ELEMENT_TYPE_GESTURE_CONTROL: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); uint8_t* data = get_element_data_buffer(pack, handle, type_buf, type_len); if (data_len != 5) { log_info("gesture control decode failed: length mismatch (%d/1)", data_len); } else { handle_gesture_control(*data, data + 1); } } break; case ELEMENT_TYPE_WATCHCONFIG: { int data_len = get_element_data_size(pack, handle, type_buf, type_len); uint8_t* data = get_element_data_buffer(pack, handle, type_buf, type_len); log_info("Set Watch UI Config %d/%d", data_len, (int)sizeof(ui_config)); //if (data_len >= (int)sizeof(ui_config)) handle_set_watch_config((ui_config*)(data + 1)); } break; case ELEMENT_TYPE_UNLOCK_WATCH: handle_unlock_watch(); break; case ELEMENT_TYPE_DAILY_ACTIVITY: handle_daily_activity(); break; } handle = get_next_element(pack, handle); } }
static DBusHandlerResult path_message_func (DBusConnection *connection, DBusMessage *message, void *user_data) { if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "Echo")) return handle_echo (connection, message); else if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "DelayEcho")) return handle_delay_echo (connection, message); else if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "Exit")) { quit (); return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "EmitFoo")) { /* Emit the Foo signal */ DBusMessage *signal; double v_DOUBLE; _dbus_verbose ("emitting signal Foo\n"); signal = dbus_message_new_signal ("/org/freedesktop/TestSuite", "org.freedesktop.TestSuite", "Foo"); if (signal == NULL) die ("No memory\n"); v_DOUBLE = 42.6; if (!dbus_message_append_args (signal, DBUS_TYPE_DOUBLE, &v_DOUBLE, DBUS_TYPE_INVALID)) die ("No memory"); if (!dbus_connection_send (connection, signal, NULL)) die ("No memory\n"); return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "RunHelloFromSelf")) { return handle_run_hello_from_self (connection, message); } else if (dbus_message_is_method_call (message, "org.freedesktop.TestSuite", "HelloFromSelf")) { DBusMessage *reply; printf ("Received the HelloFromSelf message\n"); reply = dbus_message_new_method_return (message); if (reply == NULL) die ("No memory"); if (!dbus_connection_send (connection, reply, NULL)) die ("No memory"); return DBUS_HANDLER_RESULT_HANDLED; } else return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; }
int main(int argc,char ** argv){ struct sockaddr_in server; struct sockaddr_in client; char message[MAXLEN+1]; int clients[MAXCLIENTS]; int sockfd; struct timeval tv; struct sigaction act; fd_set rset; int i,rc,len,max; len = sizeof(client); bzero(&act,sizeof(act)); act.sa_handler = int_handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGINT,&act,NULL); if( (listenfd = socket(AF_INET,SOCK_STREAM,0)) < 0){ perror(">>> socket"); exit(1); } bzero(&server,sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(10000); server.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(listenfd,(struct sockaddr *) &server,sizeof(server)) < 0){ perror(">>> bind"); exit(1); } if( listen(listenfd,LISTEN) < 0){ perror(">>> listen"); exit(1); } for(i = 0; i < MAXCLIENTS; i++) clients[i] = -1; for(;;){ FD_ZERO(&rset); FD_SET(listenfd,&rset); max = listenfd; for(i = 0; i < MAXCLIENTS; i++){ if (clients[i] == -1) continue; FD_SET(clients[i],&rset); if(max < clients[i]) max = clients[i]; } tv.tv_sec = 5; tv.tv_usec = 0; rc = select(max + 1,&rset,NULL,NULL,&tv); if(rc == 0){ printf(">>> select timeout\n"); continue; } if(rc == -1){ if(errno == EINTR){ perror(">>> 'select' was interrupted, try again"); continue; }else{ perror(">>> exit - some other problem"); exit(1); } } if (FD_ISSET(listenfd,&rset)){ if( (sockfd = accept(listenfd,(struct sockaddr *) NULL,NULL)) < 0){ if(errno == EINTR){ perror(">>> 'accept' was interrupted, accept again"); continue; }else{ perror(">>> exit - some other problem "); exit(1); } } for(i = 0; i < MAXCLIENTS; i++){ if(clients[i] == -1){ if( getpeername(sockfd,(struct sockaddr*)&client,&len) < 0){ perror(">>> getpeername"); exit(1); }else{ printf(">>> open connection from addr %s port %d\n", inet_ntop(AF_INET,&client.sin_addr,message, sizeof(message)),ntohs(client.sin_port)); } clients[i] = sockfd; break; } if(i == MAXCLIENTS - 1){ printf(">>> Clients queue is full - drop client\n"); close(sockfd); } } } for(i = 0; i < MAXCLIENTS; i++){ if (clients[i] == -1) continue; if (! FD_ISSET(clients[i],&rset)) continue; rc = handle_echo(clients[i]); if( getpeername(clients[i],(struct sockaddr*)&client,&len) < 0){ perror(">>> getpeername"); exit(1); } if(rc < 0){ perror(">>> problem with socket"); printf(">>> closing this connection: addr %s port %d\n", inet_ntop(AF_INET,&client.sin_addr,message,sizeof(message)), ntohs(client.sin_port)); close(clients[i]); clients[i] = -1; } if(rc == 0){ printf(">>> client gone,closing connection: addr %s port %d\n", inet_ntop(AF_INET,&client.sin_addr,message,sizeof(message)), ntohs(client.sin_port)); close(clients[i]); clients[i] = -1; } if(rc > 0){ printf(">>> session OK: addr %s port %d\n", inet_ntop(AF_INET,&client.sin_addr,message,sizeof(message)), ntohs(client.sin_port)); } } } close(listenfd); }