<commit_before>#include <pebble.h>
#include <datatypes.h>
#include <event_db.h>
#include <main.h>
#include <communication.h>

//Version of the watchapp. Will be compared to what version the (updated) phone app expects
#define WATCHAPP_VERSION 4
	
//Definitions of dictionary keys
#define DICT_KEY_COMMAND 0
#define DICT_KEY_VERSION 1
#define DICT_KEY_NUM_EVENTS 10
#define DICT_KEY_TITLE 1
#define DICT_KEY_LOCATION 2
#define DICT_KEY_STARTTIME 20
#define DICT_KEY_ENDTIME 30
#define DICT_KEY_ALLDAY 5
#define DICT_KEY_SETTINGS_BOOLFLAGS 40
#define DICT_KEY_SETTINGS_DESIGN 41

//Outgoing dictionary keys
#define DICT_OUT_KEY_VERSION 0

//Commands from phone
#define COMMAND_INIT_DATA 0
#define COMMAND_EVENT 1
#define COMMAND_DONE 2
#define COMMAND_EVENT_TIME 3

CalendarEvent **buffer = 0; //buffered calendar events so far
uint8_t buffer_size = 0; //number of elements in the buffer (for cleanup)
uint8_t number_received = 0; //number of events completely received
uint8_t number_expected = 0; //number of events the phone said it will send
bool expecting_event_time = 0; //if true, we expect the last received event's time now (second message)
bool update_request_sent = 0; //whether or not we informed the phone about outdated version

void send_sync_request() { //Sends a request for fresh data to the phone
	APP_LOG(APP_LOG_LEVEL_DEBUG, "Sending sync request");
	DictionaryIterator *iter;
	app_message_outbox_begin(&iter);
	Tuplet value = TupletInteger(DICT_OUT_KEY_VERSION, WATCHAPP_VERSION);
	dict_write_tuplet(iter, &value);
	app_message_outbox_send();
	sync_layer_set_progress(0,1);
}

void out_sent_handler(DictionaryIterator *sent, void *context) {
	// outgoing message was delivered
}


void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) {
	// outgoing message failed
}


void in_received_handler(DictionaryIterator *received, void *context) {
	Tuple *command = dict_find(received, DICT_KEY_COMMAND);
	
	if (command) {
		//APP_LOG(APP_LOG_LEVEL_DEBUG, "Got message with command %d", (int) command->value->uint8);
		switch (command->value->uint8) {
			case COMMAND_INIT_DATA: //First message (starting new sync, giving settings)
			//Check version number
			if (dict_find(received, DICT_KEY_VERSION)->value->uint8 != WATCHAPP_VERSION) {
				if (!update_request_sent) {
					send_sync_request(); //make sure the phone knows about our mismatched version
					update_request_sent = true; //do it only once
				}
				return; //ignore message
			}
			
			if (number_expected != 0) {
				communication_cleanup();
			}
			number_expected = dict_find(received, DICT_KEY_NUM_EVENTS)->value->uint8;
			if (number_expected != 0) {
				//init buffer
				number_received = 0;
				buffer_size = 0;
				buffer = malloc(sizeof(CalendarEvent*)*number_expected);
				APP_LOG(APP_LOG_LEVEL_DEBUG, "Starting sync. Expecting %d events", (int) number_expected);
			}
			
			//Apply settings from the message
			settings_set(dict_find(received, DICT_KEY_SETTINGS_BOOLFLAGS)->value->uint32, 
						 dict_find(received, DICT_KEY_SETTINGS_DESIGN)->value->uint32);
			
			//Begin heightened communication status (for faster sync, hopefully)
			app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED);
			
			//Show user
			sync_layer_set_progress(number_received+1, number_expected+2);
			break;
			<commit_msg>Better version checking<commit_after>#include <pebble.h>
#include <datatypes.h>
#include <event_db.h>
#include <main.h>
#include <communication.h>

//Version of the watchapp. Will be compared to what version the (updated) phone app expects
#define WATCHAPP_VERSION 5
#define BACKWARD_COMPAT_VERSION 4
//BACKWARD_COMPAT_VERSION smallest version number that this version is backwards compatible to (so an Android app bundling that (older) version would still work)
	
//Definitions of dictionary keys
#define DICT_KEY_COMMAND 0
#define DICT_KEY_VERSION 1
#define DICT_KEY_NUM_EVENTS 10
#define DICT_KEY_TITLE 1
#define DICT_KEY_LOCATION 2
#define DICT_KEY_STARTTIME 20
#define DICT_KEY_ENDTIME 30
#define DICT_KEY_ALLDAY 5
#define DICT_KEY_SETTINGS_BOOLFLAGS 40
#define DICT_KEY_SETTINGS_DESIGN 41

//Outgoing dictionary keys
#define DICT_OUT_KEY_VERSION 0
#define DICT_OUT_KEY_BACKWARDSVERSION 1

//Commands from phone
#define COMMAND_INIT_DATA 0
#define COMMAND_EVENT 1
#define COMMAND_DONE 2
#define COMMAND_EVENT_TIME 3

CalendarEvent **buffer = 0; //buffered calendar events so far
uint8_t buffer_size = 0; //number of elements in the buffer (for cleanup)
uint8_t number_received = 0; //number of events completely received
uint8_t number_expected = 0; //number of events the phone said it will send
bool expecting_event_time = 0; //if true, we expect the last received event's time now (second message)
bool update_request_sent = 0; //whether or not we informed the phone about outdated version

void send_sync_request() { //Sends a request for fresh data to the phone
	APP_LOG(APP_LOG_LEVEL_DEBUG, "Sending sync request");
	DictionaryIterator *iter;
	app_message_outbox_begin(&iter);
	Tuplet value = TupletInteger(DICT_OUT_KEY_VERSION, WATCHAPP_VERSION);
	dict_write_tuplet(iter, &value);
	Tuplet value2 = TupletInteger(DICT_OUT_KEY_BACKWARDSVERSION, BACKWARD_COMPAT_VERSION);
	dict_write_tuplet(iter, &value2);
	app_message_outbox_send();
	sync_layer_set_progress(0,1);
}

void out_sent_handler(DictionaryIterator *sent, void *context) {
	// outgoing message was delivered
}


void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) {
	// outgoing message failed
}


void in_received_handler(DictionaryIterator *received, void *context) {
	Tuple *command = dict_find(received, DICT_KEY_COMMAND);
	
	if (command) {
		//APP_LOG(APP_LOG_LEVEL_DEBUG, "Got message with command %d", (int) command->value->uint8);
		switch (command->value->uint8) {
			case COMMAND_INIT_DATA: //First message (starting new sync, giving settings)
			//Check version number
			if (dict_find(received, DICT_KEY_VERSION)->value->uint8 > WATCHAPP_VERSION) {
				if (!update_request_sent) {
					send_sync_request(); //make sure the phone knows about our mismatched version
					update_request_sent = true; //do it only once
				}
				return; //ignore message
			}
			
			if (number_expected != 0) {
				communication_cleanup();
			}
			number_expected = dict_find(received, DICT_KEY_NUM_EVENTS)->value->uint8;
			if (number_expected != 0) {
				//init buffer
				number_received = 0;
				buffer_size = 0;
				buffer = malloc(sizeof(CalendarEvent*)*number_expected);
				APP_LOG(APP_LOG_LEVEL_DEBUG, "Starting sync. Expecting %d events", (int) number_expected);
			}
			
			//Apply settings from the message
			settings_set(dict_find(received, DICT_KEY_SETTINGS_BOOLFLAGS)->value->uint32, 
						 dict_find(received, DICT_KEY_SETTINGS_DESIGN)->value->uint32);
			
			//Begin heightened communication status (for faster sync, hopefully)
			app_comm_set_sniff_interval(SNIFF_INTERVAL_REDUCED);
			
			//Show user
			sync_layer_set_progress(number_received+1, number_expected+2);
			break;
			
{'fraction_non_alphanumeric': 0.08712336554860717, 'fraction_numerical': 0.013644115974985787, 'mean_word_length': 5.140488656195463, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7444763', 'n_tokens_mistral': 2537, 'n_tokens_neox': 2221, 'n_words': 640}