int main() { app_message_register_outbox_sent(data_delivered); app_message_register_inbox_received(received_data); app_message_open(124, 50); DictionaryIterator *iterator; app_message_outbox_begin(&iterator); dict_write_uint8(iterator, 0, 0); dict_write_uint8(iterator, 1, 0); dict_write_uint16(iterator, 2, PROTOCOL_VERSION); #ifdef PBL_PLATFORM_APLITE dict_write_uint8(iterator, 3, 0); #else dict_write_uint8(iterator, 3, 1); #endif app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED); app_message_outbox_send(); switchWindow(0); app_event_loop(); window_stack_pop_all(false); return 0; }
void sendScore(uint16_t score) { // Begin dictionary DictionaryIterator *iter; app_message_outbox_begin(&iter); // Add a key-value pair DictionaryResult result = dict_write_uint16(iter, 1, score); switch(result) { case DICT_OK: APP_LOG(APP_LOG_LEVEL_INFO, "nothing went wrong in message construction"); break; case DICT_INVALID_ARGS: case DICT_MALLOC_FAILED: case DICT_INTERNAL_INCONSISTENCY: case DICT_NOT_ENOUGH_STORAGE: default: APP_LOG(APP_LOG_LEVEL_INFO, "something went wrong in message construction"); break; } uint32_t size = dict_write_end(iter); APP_LOG(APP_LOG_LEVEL_INFO, "size of dictionary: %u", (unsigned int)size); // Send the message! app_message_outbox_send(); }
static void requestNumbers(uint16_t pos) { DictionaryIterator *iterator; app_message_outbox_begin(&iterator); dict_write_uint8(iterator, 0, 2); dict_write_uint8(iterator, 1, 0); dict_write_uint16(iterator, 2, pos); app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED); app_message_outbox_send(); }
static void send_initial_packet() { DictionaryIterator *iterator; app_message_outbox_begin(&iterator); dict_write_uint8(iterator, 0, 0); dict_write_uint8(iterator, 1, 0); dict_write_uint16(iterator, 2, PROTOCOL_VERSION); dict_write_uint32(iterator, 3, getCapabilities(appmessage_max_size)); app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED); app_message_outbox_send(); }
void accel_tap_handler(AccelAxisType axis, int32_t direction) { time_t time_tope = time(NULL); // Change Display text_layer_set_text(info_layer, "Tope!"); // Send Message to Mobile App DictionaryIterator *iter; app_message_outbox_begin(&iter); dict_write_uint8( iter, 0, MESSAGE_TYPE_TOPE_EVENT); dict_write_uint16( iter, 1, time_tope); app_message_outbox_send(); }
void data_framework_ask_for_weather_with_manners(){ DictionaryIterator *iter; app_message_outbox_begin(&iter); if (iter == NULL) { return; } dict_write_uint16(iter, 200, 202); dict_write_end(iter); app_message_outbox_send(); }
void send_weather_request(){ DictionaryIterator *iter; app_message_outbox_begin(&iter); if (iter == NULL) { return; } dict_write_uint16(iter, 200, 202); dict_write_end(iter); app_message_outbox_send(); }
static void select_click_handler(ClickRecognizerRef recognizer, void *context) { text_layer_set_text(text_layer, "Message Sent!"); DictionaryIterator *iter; app_message_outbox_begin(&iter); if (iter == NULL) { return; } dict_write_uint16(iter, firstKey, 1); dict_write_end(iter); app_message_outbox_send(); }
static void select_click_handler(ClickRecognizerRef recognizer, void *context) { text_layer_set_text(text_layer, "Send Message"); DictionaryIterator *iter; app_message_outbox_begin(&iter); if (iter == NULL) { return; } dict_write_uint16(iter, 200, messageIdentifier); // 200 and mtype are indentifiers, 1(messageIdentifier) is value. dict_write_end(iter); app_message_outbox_send(); updateSent(); }
void get_settings(){ DictionaryIterator *iter; time_t now = time(NULL); struct tm *currentTime = localtime(&now); int year = currentTime->tm_year; int month = currentTime->tm_mon+offset; factorDate(&month, &year); if (app_message_outbox_begin(&iter) != APP_MSG_OK) { app_log(APP_LOG_LEVEL_DEBUG, "calendarApp.c",364,"App MSG Not ok"); return; } if (dict_write_uint8(iter, GET_SETTINGS, ((uint8_t)0)) != DICT_OK) { app_log(APP_LOG_LEVEL_DEBUG, "calendarApp.c",364,"Dict Not ok"); return; } if (dict_write_uint8(iter, APP_VERSION_MAJOR, ((uint8_t)__pbl_app_info.app_version.major)) != DICT_OK) { app_log(APP_LOG_LEVEL_DEBUG, "calendarApp.c",364,"Dict Not ok"); return; } if (dict_write_uint8(iter, APP_VERSION_MINOR, ((uint8_t)__pbl_app_info.app_version.minor)) != DICT_OK) { app_log(APP_LOG_LEVEL_DEBUG, "calendarApp.c",364,"Dict Not ok"); return; } if(year*100+month>0){ if (dict_write_uint16(iter, GET_EVENT_DAYS, ((uint16_t)year*100+month)) != DICT_OK) { app_log(APP_LOG_LEVEL_DEBUG, "calendarApp.c",364,"Dict Not ok"); return; } } if(watchmode){ if (dict_write_uint8(iter, GET_EVENT_DETAILS, ((uint8_t)1)) != DICT_OK) { app_log(APP_LOG_LEVEL_DEBUG, "agendaWindow.c",364,"Dict Not ok"); return; } } if (app_message_outbox_send() != APP_MSG_OK){ app_log(APP_LOG_LEVEL_DEBUG, "calendarApp.c",364,"Message Not Sent"); return; } app_log(APP_LOG_LEVEL_DEBUG, "calendarApp.c",364,"Message Sent"); }
void activate_gesture(int16_t gesture_type) { vibes_short_pulse(); app_message_register_outbox_sent(on_send_success); app_message_register_outbox_failed(on_send_failed); app_message_open(64, 64); DictionaryIterator *iter; if (!check_appmessage_result(app_message_outbox_begin(&iter))) return; static uint8_t outbox_buffer[50]; switch (dict_write_begin(iter, outbox_buffer, 50)) { case DICT_OK: break; case DICT_INVALID_ARGS: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_INVALID_ARGS"); return; case DICT_INTERNAL_INCONSISTENCY: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_INTERNAL_INCONSISTENCY"); return; case DICT_MALLOC_FAILED: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_MALLOC_FAILED"); return; case DICT_NOT_ENOUGH_STORAGE: APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: DICT_NOT_ENOUGH_STORAGE"); return; } dict_write_uint16(iter, 0x0, gesture_type); if (dict_write_end(iter) == 0) { APP_LOG(APP_LOG_LEVEL_ERROR, "send_alert_to_phone: ERROR ENDING DICTIONARY"); return; } check_appmessage_result(app_message_outbox_send()); }
void note_data_received(DictionaryIterator* iterator){ Tuple *t = dict_read_first(iterator); // For all items while(t != NULL) { // Which key was received? APP_LOG(APP_LOG_LEVEL_ERROR, "Key %d ", (int)t->key); // Look for next item t = dict_read_next(iterator); } uint16_t location = dict_find(iterator, 1)->value->uint16; uint16_t segmentLength = dict_find(iterator, 2)->value->uint16; memcpy((void *) &fullNote[location], dict_find(iterator, 4)->value->cstring, segmentLength); GSize maxSize = graphics_text_layout_get_content_size(fullNote, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD), GRect(0, 0, 144, 1000), GTextOverflowModeWordWrap, GTextAlignmentLeft); if (maxSize.h < 168 - 16) maxSize.h = 168 - 16; text_layer_set_size(fullNoteText, maxSize); scroll_layer_set_content_size(scroller, maxSize); text_layer_set_text(fullNoteText, fullNote); if (segmentLength == 75){ DictionaryIterator *iterator; app_message_outbox_begin(&iterator); dict_write_uint8(iterator, 0, 3); dict_write_uint16(iterator, 1, location + 75); app_message_outbox_send(); app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED); app_comm_set_sniff_interval(SNIFF_INTERVAL_NORMAL); } }
void whatsapp_request_conversations() { DictionaryIterator *iter; app_message_outbox_begin(&iter); if (iter == NULL) return; dict_write_uint8(iter, KEY_ACTION, (uint8_t) ACTION_WHATSAPP_REQUEST_CONVERSATIONS); dict_write_uint16(iter, KEY_VERSION_CODE, (uint16_t)(VERSION_CODE)); dict_write_end(iter); // HACK //app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED); //app_comm_set_sniff_interval(SNIFF_INTERVAL_NORMAL); app_message_outbox_send(); whatsapp_loading = true; num_conversations = 0; current_request_id = 0xFF; }
void light_init(void) { timer = app_timer_register(1000, timer_callback, NULL); all_lights = malloc(sizeof(Light)); all_lights->index = 0; strncpy(all_lights->label, "All Lights", sizeof(all_lights->label) - 1); all_lights->color = (Color) { .hue = 50, .saturation = 100, .brightness = 100, .kelvin = 3500, }; lightlist_init(); light_update_settings(); } void light_deinit(void) { if (error) free(error); if (all_lights) free(all_lights); if (lights) free(lights); if (tags) free(tags); lightlist_deinit(); } void light_in_received_handler(DictionaryIterator *iter) { if (!dict_find(iter, KEY_TYPE)) return; if (error) { free(error); error = NULL; } switch (dict_find(iter, KEY_TYPE)->value->uint8) { case KEY_TYPE_ERROR: { error = malloc(dict_find(iter, KEY_LABEL)->length); strncpy(error, dict_find(iter, KEY_LABEL)->value->cstring, dict_find(iter, KEY_LABEL)->length - 1); LOG("error: %s", error); all_menu_layer_reload_data_and_mark_dirty(); break; } case KEY_TYPE_LIGHT: switch (dict_find(iter, KEY_METHOD)->value->uint8) { case KEY_METHOD_BEGIN: if (lights) free(lights); num_lights = dict_find(iter, KEY_INDEX)->value->uint8; lights = malloc(sizeof(Light) * num_lights); break; case KEY_METHOD_END: all_menu_layer_reload_data_and_mark_dirty(); break; case KEY_METHOD_DATA: { uint8_t index = dict_find(iter, KEY_INDEX)->value->uint8; Light *light = &lights[index]; light->index = index; strncpy(light->label, dict_find(iter, KEY_LABEL)->value->cstring, sizeof(light->label) - 1); strncpy(light->state, dict_find(iter, KEY_STATE)->value->cstring, sizeof(light->state) - 1); light->color = (Color) { .hue = dict_find(iter, KEY_COLOR_H)->value->uint8, .saturation = dict_find(iter, KEY_COLOR_S)->value->uint8, .brightness = dict_find(iter, KEY_COLOR_B)->value->uint8, .kelvin = dict_find(iter, KEY_COLOR_K)->value->uint16, }; LOG("light: %d '%s' '%s' %d %d %d %d", light->index, light->label, light->state, light->color.hue, light->color.saturation, light->color.brightness, light->color.kelvin); all_menu_layer_reload_data_and_mark_dirty(); break; } } break; case KEY_TYPE_TAG: switch (dict_find(iter, KEY_METHOD)->value->uint8) { case KEY_METHOD_BEGIN: if (tags) free(tags); num_tags = dict_find(iter, KEY_INDEX)->value->uint8; tags = malloc(sizeof(Light) * num_tags); break; case KEY_METHOD_END: all_menu_layer_reload_data_and_mark_dirty(); break; case KEY_METHOD_DATA: { uint8_t index = dict_find(iter, KEY_INDEX)->value->uint8; Light *tag = &tags[index]; tag->index = index; strncpy(tag->label, dict_find(iter, KEY_LABEL)->value->cstring, sizeof(tag->label) - 1); strncpy(tag->state, "", sizeof(tag->state) - 1); tag->color = (Color) { .hue = dict_find(iter, KEY_COLOR_H)->value->uint8, .saturation = dict_find(iter, KEY_COLOR_S)->value->uint8, .brightness = dict_find(iter, KEY_COLOR_B)->value->uint8, .kelvin = dict_find(iter, KEY_COLOR_K)->value->uint16, }; LOG("tag: %d '%s' '%s' %d %d %d %d", tag->index, tag->label, tag->state, tag->color.hue, tag->color.saturation, tag->color.brightness, tag->color.kelvin); all_menu_layer_reload_data_and_mark_dirty(); break; } } break; } } void light_out_sent_handler(DictionaryIterator *sent) { } void light_out_failed_handler(DictionaryIterator *failed, AppMessageResult reason) { if (error) free(error); error = malloc(sizeof(char) * 65); strncpy(error, "Unable to connect to phone! Make sure the Pebble app is running.", 64); LOG("error: %s", error); all_menu_layer_reload_data_and_mark_dirty(); } void light_update_settings() { menu_section_lights = settings()->tags_first ? 2 : 1; menu_section_tags = settings()->tags_first ? 1 : 2; menu_section_lights = settings()->hide_lights ? 88 : menu_section_lights; menu_section_tags = settings()->hide_tags ? 89 : menu_section_tags; all_menu_layer_reload_data_and_mark_dirty(); } void light_refresh() { DictionaryIterator *iter; app_message_outbox_begin(&iter); dict_write_uint8(iter, KEY_METHOD, KEY_METHOD_REFRESH); dict_write_end(iter); app_message_outbox_send(); } void light_toggle() { if (selected_type == KEY_TYPE_LIGHT) strncpy(light()->state, "...", sizeof(light()->state) - 1); all_menu_layer_reload_data_and_mark_dirty(); DictionaryIterator *iter; app_message_outbox_begin(&iter); dict_write_uint8(iter, KEY_METHOD, KEY_METHOD_TOGGLE); dict_write_uint8(iter, KEY_TYPE, selected_type); dict_write_uint8(iter, KEY_INDEX, selected_index); dict_write_end(iter); app_message_outbox_send(); } void light_update_color() { DictionaryIterator *iter; app_message_outbox_begin(&iter); dict_write_uint8(iter, KEY_METHOD, KEY_METHOD_COLOR); dict_write_uint8(iter, KEY_TYPE, selected_type); dict_write_uint8(iter, KEY_INDEX, selected_index); dict_write_uint8(iter, KEY_COLOR_H, light()->color.hue); dict_write_uint8(iter, KEY_COLOR_S, light()->color.saturation); dict_write_uint8(iter, KEY_COLOR_B, light()->color.brightness); dict_write_uint16(iter, KEY_COLOR_K, light()->color.kelvin); dict_write_end(iter); app_message_outbox_send(); }
static void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) { // Log Error time_t now = time(NULL); struct tm *clock_time = localtime(&now); char error_time_text[] = "00:00:00"; strftime(error_time_text, sizeof(error_time_text), "%T", clock_time); if (reason == APP_MSG_OK) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_OK", error_time_text); } else if (reason == APP_MSG_SEND_TIMEOUT) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_SEND_TIMEOUT", error_time_text); } else if (reason == APP_MSG_SEND_REJECTED) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_SEND_REJECTED", error_time_text); } else if (reason == APP_MSG_NOT_CONNECTED) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_NOT_CONNECTED", error_time_text); } else if (reason == APP_MSG_APP_NOT_RUNNING) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_APP_NOT_RUNNING", error_time_text); } else if (reason == APP_MSG_INVALID_ARGS) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_INVALID_ARGS", error_time_text); } else if (reason == APP_MSG_BUSY) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_BUSY", error_time_text); } else if (reason == APP_MSG_BUFFER_OVERFLOW) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_BUFFER_OVERFLOW", error_time_text); } else if (reason == APP_MSG_ALREADY_RELEASED) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_ALREADY_RELEASED", error_time_text); } else if (reason == APP_MSG_CALLBACK_ALREADY_REGISTERED) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_CALLBACK_ALREADY_REGISTERED", error_time_text); } else if (reason == APP_MSG_CALLBACK_NOT_REGISTERED) { APP_LOG(APP_LOG_LEVEL_DEBUG, "[%s] App Message out_failed_handler: APP_MSG_CALLBACK_NOT_REGISTERED", error_time_text); } // Error Handling if ((reason == APP_MSG_SEND_TIMEOUT || reason == APP_MSG_BUSY) && bluetooth_connection_service_peek()) { // Try Reseanding Message // Create New Output Iterator DictionaryIterator *iterator; if (app_message_outbox_begin(&iterator) != APP_MSG_OK) { return; } // For Each Tuple in Failed Dictionary Iterator, Read Value, Classify Value, and Write to New Iterator Tuple *tuple = dict_read_first(failed); while (tuple) { switch (tuple->type) { case TUPLE_BYTE_ARRAY: dict_write_data (iterator, tuple->key, tuple->value->data, tuple->length); break; case TUPLE_CSTRING: dict_write_cstring(iterator, tuple->key, tuple->value->cstring); break; case TUPLE_UINT: if (tuple->length == 1) { dict_write_uint8(iterator, tuple->key, tuple->value->uint8); } else if (tuple->length == 2) { dict_write_uint16(iterator, tuple->key, tuple->value->uint16); } else { dict_write_uint32(iterator, tuple->key, tuple->value->uint32); } break; case TUPLE_INT: if (tuple->length == 1) { dict_write_int8(iterator, tuple->key, tuple->value->int8); } else if (tuple->length == 2) { dict_write_int16(iterator, tuple->key, tuple->value->int16); } else { dict_write_int32(iterator, tuple->key, tuple->value->int32); } break; default: break; } tuple = dict_read_next(failed); } // Resend App Message app_message_outbox_send(); } }
void register_uint16(AppKey type, uint16_t i) { dict_write_uint16(dict, type, i); }