1 | /****************************************************************************************
|
---|
2 | *
|
---|
3 | * cmd_rtc.c
|
---|
4 | *
|
---|
5 | * óïðàâëåíèå ÷àñàìè è SNTP
|
---|
6 | ****************************************************************************************/
|
---|
7 |
|
---|
8 | #include "commands.h"
|
---|
9 | #include "main.h"
|
---|
10 | #include "Time.h"
|
---|
11 | #include "sntp.h"
|
---|
12 | #include <string.h>
|
---|
13 |
|
---|
14 | extern RealTimeClock_typeDef RealTimeClock;
|
---|
15 |
|
---|
16 | void cmd_getRTC(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
17 | {
|
---|
18 | json_write_obj_string("status", "ok");
|
---|
19 | json_write_obj_int("time", time_to_unix(&RealTimeClock));
|
---|
20 | if(RealTimeClock.Flags.BAT_LOW) json_write_obj_bool("bl", RealTimeClock.Flags.BAT_LOW);
|
---|
21 |
|
---|
22 | json_write_obj_string("ip", ip4addr_ntoa(sntp_getserver(0)));
|
---|
23 | json_write_obj_int("stratum", sntp_getstratum(0));
|
---|
24 | json_write_obj_int("poll", sntp_getpool(0) / 1000);
|
---|
25 | json_write_obj_int("reach", sntp_getreachability(0));
|
---|
26 | }
|
---|
27 |
|
---|
28 | void cmd_setRTC(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
29 | {
|
---|
30 | RealTimeClock_typeDef t;
|
---|
31 | int32_t time;
|
---|
32 | if(json_get_value_object(tokens_data, "time", &time, json_type_integer, json_root)== json_success){
|
---|
33 | json_write_obj_string("status", "ok");
|
---|
34 | unix_to_time(&t, time);
|
---|
35 | SetCurrent_RTC(&t);
|
---|
36 | }
|
---|
37 | else {
|
---|
38 | json_write_obj_string("status", "error");
|
---|
39 | json_write_obj_string("error", "invalid parametr");
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | void cmd_getRTCcorr(json_tokens_data_t *tokens_data, json_response_t *json_response){
|
---|
44 | json_write_obj_string("status", "ok");
|
---|
45 | json_write_obj_int("corr", RealTimeClock.Correction);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void cmd_setRTCcorr(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
49 | {
|
---|
50 | int8_t corr;
|
---|
51 | if(json_get_value_object(tokens_data, "corr", &corr, json_type_integer, json_root)== json_success){
|
---|
52 | json_write_obj_string("status", "ok");
|
---|
53 | SetCorrection_RTC(&RealTimeClock, corr);
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | json_write_obj_string("status", "error");
|
---|
57 | json_write_obj_string("error", "invalid parametr");
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | void cmd_setNTP(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
62 | {
|
---|
63 | ip_addr_t sntp_addr;
|
---|
64 | char str_sntp_addr[16] = {0};
|
---|
65 | if(json_get_value_object(tokens_data, "ip", str_sntp_addr, json_type_string, json_root)== json_success){
|
---|
66 | sntp_stop();
|
---|
67 | if(strlen(str_sntp_addr) != 0){
|
---|
68 | ip4addr_aton(str_sntp_addr, &sntp_addr);
|
---|
69 | sntp_setserver(0, &sntp_addr);
|
---|
70 | sntp_init();
|
---|
71 | }
|
---|
72 | else sntp_setserver(0, NULL);
|
---|
73 | json_write_obj_string("status", "ok");
|
---|
74 | }
|
---|
75 | else {
|
---|
76 | json_write_obj_string("status", "error");
|
---|
77 | json_write_obj_string("error", "invalid parametr");
|
---|
78 | }
|
---|
79 | }
|
---|