Commits

Trammell Hudson  committed 41c5ac8

authenticator works

  • Participants
  • Parent commits 117ec67

Comments (0)

Files changed (1)

 );
 
 
+static char time_buffer[32];
+static char otp_buffer[8];
+static Window window;
+static TextLayer time_layer;
+static TextLayer otp_layer;
+
+#define GMT_OFFSET 5
+
+static uint32_t unix_epoch_time(
+	const PblTm * const curr_time
+)
+{
+  long now = 0;
+  now += (curr_time->tm_year-70)*31536000;
+  now += ((curr_time->tm_year-69)/4)*86400;
+  now -= ((curr_time->tm_year-1)/100)*86400;
+  now += ((curr_time->tm_year+299)/400)*86400;
+  now += curr_time->tm_yday*86400;
+  now += curr_time->tm_hour*3600;
+  now += curr_time->tm_min*60;
+  now += curr_time->tm_sec;
+  return now;
+}
 
 static void
 handle_tick(
 	PebbleTickEvent * const event
 )
 {
+	(void) ctx;
+
+/*
 	string_format_time(
 		time_buffer,
 		sizeof(time_buffer),
 		event->tick_time
 	);
 
-	text_layer_set_text(&s_text, time_buffer);
+	text_layer_set_text(&time_layer, time_buffer);
+*/
+
+	// Convert the time into a unix epoch time.
+	// this should be done in a library
+	const PblTm * const ptm = event->tick_time;
+
+/*
+	struct tm t = {
+		.tm_year = ptm->tm_year,
+		.tm_mon = ptm->tm_mon,
+		.tm_mday = ptm->tm_mday,
+		.tm_hour = ptm->tm_hour,
+		.tm_min = ptm->tm_min,
+		.tm_sec = ptm->tm_sec,
+	};
+	const uint32_t now = mktime(&t);
+*/
+	const uint32_t now = unix_epoch_time(ptm) + 3600 * GMT_OFFSET;
+	uint32_t auth = oauth_calc(now);
+	otp_buffer[6] = 0;
+	otp_buffer[5] = '0' + auth % 10; auth /= 10;
+	otp_buffer[4] = '0' + auth % 10; auth /= 10;
+	otp_buffer[3] = '0' + auth % 10; auth /= 10;
+	otp_buffer[2] = '0' + auth % 10; auth /= 10;
+	otp_buffer[1] = '0' + auth % 10; auth /= 10;
+	otp_buffer[0] = '0' + auth % 10; auth /= 10;
+
+	text_layer_set_text(&otp_layer, otp_buffer);
 }
 
 
 	(void) ctx;
 
 	window_init(&window, "RFC 6238");
-	window_stack_push(&s_window, true);
+	window_stack_push(&window, true);
 
-	text_layer_init(&text_layer, window.layer.frame);
-	text_layer_set(&text_layer, time_buffer);
-	layer_add_child(&wndow.layer, &text_layer.layer);
+	//text_layer_init(&time_layer, window.layer.frame);
+	time_buffer[0] = '\0';
+	//text_layer_set_text(&time_layer, time_buffer);
+	//layer_add_child(&window.layer, &time_layer.layer);
+
+	text_layer_init(&otp_layer, window.layer.frame);
+	otp_buffer[0] = '\0';
+	text_layer_set_text(&otp_layer, otp_buffer);
+	layer_add_child(&window.layer, &otp_layer.layer);
 }
 
 
-static const PebbleAppHandlers handlers = {
-	.init_handler	= &handle_init,
-	.tick_info	= {
-		.tick_handler = &handle_tick,
-		.tick_units = SECOND_UNIT,
-	},
-};
-
-extern "C"
-void pbl_main(
+void
+pbl_main(
 	void * const params
 )
 {
+	PebbleAppHandlers handlers = {
+		.init_handler	= &handle_init,
+		.tick_info	= {
+			.tick_handler = &handle_tick,
+			.tick_units = SECOND_UNIT,
+		},
+	};
+
 	app_event_loop(params, &handlers);
 }