research application on sending data to headend
Dependencies: DataStore JobScheduler NetworkServices W5500Interface nanopb protocol
See "main.cpp" documentation on "API Documentation" tab for details about application.
source/main.cpp@13:0fdbc14c33e7, 2017-08-03 (annotated)
- Committer:
- sgnezdov
- Date:
- Thu Aug 03 00:07:50 2017 +0000
- Revision:
- 13:0fdbc14c33e7
- Parent:
- 12:ec140a26367f
- Child:
- 16:bef1673b199e
work in progress on reporting list of jobs; Issue: At time is zero
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sgnezdov | 0:2c57ed6943f7 | 1 | #include "mbed.h" |
sgnezdov | 0:2c57ed6943f7 | 2 | |
sgnezdov | 0:2c57ed6943f7 | 3 | #include "mbed-trace/mbed_trace.h" |
sgnezdov | 0:2c57ed6943f7 | 4 | #define TRACE_GROUP "main" |
sgnezdov | 0:2c57ed6943f7 | 5 | |
sgnezdov | 1:eebe442fc126 | 6 | #include "scheduler.h" |
sgnezdov | 1:eebe442fc126 | 7 | #include "schedules.h" |
sgnezdov | 1:eebe442fc126 | 8 | #include "jobService.h" |
sgnezdov | 4:b360d4f0bf34 | 9 | #include "netstack.h" |
sgnezdov | 13:0fdbc14c33e7 | 10 | #include "config.h" |
sgnezdov | 4:b360d4f0bf34 | 11 | #include "lceProxy.h" |
sgnezdov | 4:b360d4f0bf34 | 12 | |
sgnezdov | 1:eebe442fc126 | 13 | #include "jobTestPrint.h" |
sgnezdov | 4:b360d4f0bf34 | 14 | #include "jobFakeADC.h" |
sgnezdov | 11:acaefb63fc6b | 15 | #include "jobSchedulesUpload.h" |
sgnezdov | 1:eebe442fc126 | 16 | |
sgnezdov | 0:2c57ed6943f7 | 17 | // These are necessary only if thread safety is needed |
sgnezdov | 0:2c57ed6943f7 | 18 | static Mutex TracingLock; |
sgnezdov | 0:2c57ed6943f7 | 19 | static void tracingWait() |
sgnezdov | 0:2c57ed6943f7 | 20 | { |
sgnezdov | 0:2c57ed6943f7 | 21 | TracingLock.lock(); |
sgnezdov | 0:2c57ed6943f7 | 22 | } |
sgnezdov | 0:2c57ed6943f7 | 23 | static void tracingRelease() |
sgnezdov | 0:2c57ed6943f7 | 24 | { |
sgnezdov | 0:2c57ed6943f7 | 25 | TracingLock.unlock(); |
sgnezdov | 0:2c57ed6943f7 | 26 | } |
sgnezdov | 0:2c57ed6943f7 | 27 | |
sgnezdov | 9:d6a9210bfd41 | 28 | static bool initTime() |
sgnezdov | 9:d6a9210bfd41 | 29 | { |
sgnezdov | 9:d6a9210bfd41 | 30 | struct tm clock; |
sgnezdov | 9:d6a9210bfd41 | 31 | clock.tm_year = 2001 - 1900; |
sgnezdov | 9:d6a9210bfd41 | 32 | clock.tm_mon = 0; |
sgnezdov | 9:d6a9210bfd41 | 33 | clock.tm_mday = 1; |
sgnezdov | 9:d6a9210bfd41 | 34 | clock.tm_hour = 13; |
sgnezdov | 9:d6a9210bfd41 | 35 | clock.tm_min = 30; |
sgnezdov | 9:d6a9210bfd41 | 36 | clock.tm_sec = 0; |
sgnezdov | 9:d6a9210bfd41 | 37 | // time_t is in seconds |
sgnezdov | 9:d6a9210bfd41 | 38 | time_t time = mktime(&clock); |
sgnezdov | 9:d6a9210bfd41 | 39 | if (time == (time_t) - 1) { |
sgnezdov | 9:d6a9210bfd41 | 40 | error("Error setting clock."); |
sgnezdov | 9:d6a9210bfd41 | 41 | return false; |
sgnezdov | 9:d6a9210bfd41 | 42 | } |
sgnezdov | 9:d6a9210bfd41 | 43 | set_time(time); |
sgnezdov | 9:d6a9210bfd41 | 44 | printf("Time is set to: %s\n", ctime(&time)); |
sgnezdov | 9:d6a9210bfd41 | 45 | return true; |
sgnezdov | 9:d6a9210bfd41 | 46 | } |
sgnezdov | 9:d6a9210bfd41 | 47 | |
sgnezdov | 0:2c57ed6943f7 | 48 | int main() |
sgnezdov | 0:2c57ed6943f7 | 49 | { |
sgnezdov | 12:ec140a26367f | 50 | Serial pc(USBTX, USBRX); |
sgnezdov | 12:ec140a26367f | 51 | pc.baud(115200); |
sgnezdov | 12:ec140a26367f | 52 | |
sgnezdov | 0:2c57ed6943f7 | 53 | printf("\n==Borsch==\n"); |
sgnezdov | 9:d6a9210bfd41 | 54 | |
sgnezdov | 9:d6a9210bfd41 | 55 | // if (initTime() == false) { |
sgnezdov | 11:acaefb63fc6b | 56 | // printf("Application terminated: initTime has failed\n"); |
sgnezdov | 9:d6a9210bfd41 | 57 | // exit(1); |
sgnezdov | 9:d6a9210bfd41 | 58 | // } |
sgnezdov | 9:d6a9210bfd41 | 59 | |
sgnezdov | 0:2c57ed6943f7 | 60 | /* Setup tracing */ |
sgnezdov | 0:2c57ed6943f7 | 61 | mbed_trace_mutex_wait_function_set( tracingWait ); // only if thread safety is needed |
sgnezdov | 0:2c57ed6943f7 | 62 | mbed_trace_mutex_release_function_set( tracingRelease ); // only if thread safety is needed |
sgnezdov | 0:2c57ed6943f7 | 63 | mbed_trace_init(); // initialize the trace library |
sgnezdov | 0:2c57ed6943f7 | 64 | |
sgnezdov | 13:0fdbc14c33e7 | 65 | tr_info("**Started**"); //-> "[INFO][main]: this is an info msg" |
sgnezdov | 13:0fdbc14c33e7 | 66 | |
sgnezdov | 13:0fdbc14c33e7 | 67 | Config conf("Nucleo1"); |
sgnezdov | 2:661c545d718e | 68 | |
sgnezdov | 9:d6a9210bfd41 | 69 | uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x45, 0x56, 0x67}; |
sgnezdov | 2:661c545d718e | 70 | NetworkInterface* nif = initNetworkStack(mac_addr); |
sgnezdov | 2:661c545d718e | 71 | if (nif == NULL) { |
sgnezdov | 2:661c545d718e | 72 | tr_error("**Terminated**"); |
sgnezdov | 2:661c545d718e | 73 | exit(0); |
sgnezdov | 2:661c545d718e | 74 | } |
sgnezdov | 0:2c57ed6943f7 | 75 | |
sgnezdov | 7:c4123a87abe2 | 76 | const char* sn = "Nucleo1"; |
sgnezdov | 7:c4123a87abe2 | 77 | LceProxy lce(*nif, sn); |
sgnezdov | 4:b360d4f0bf34 | 78 | |
sgnezdov | 1:eebe442fc126 | 79 | JobScheduler::JobService js; |
sgnezdov | 1:eebe442fc126 | 80 | JobScheduler::Scheduler scheduler(&js); |
sgnezdov | 1:eebe442fc126 | 81 | |
sgnezdov | 11:acaefb63fc6b | 82 | /* Job Type ID shall match command id defined in ISOM commandTypes.go |
sgnezdov | 11:acaefb63fc6b | 83 | While this rule is not strictly necessary it cuts down on code responsible |
sgnezdov | 11:acaefb63fc6b | 84 | for mapping between our internal IDs to CommandIDs. |
sgnezdov | 11:acaefb63fc6b | 85 | |
sgnezdov | 11:acaefb63fc6b | 86 | addKV(NewCommandMetaData(1, "NO-OP")) |
sgnezdov | 11:acaefb63fc6b | 87 | addKV(NewCommandMetaData(2, "List Schedules")) |
sgnezdov | 11:acaefb63fc6b | 88 | addKV(NewCommandMetaData(3, "Add Schedule")) |
sgnezdov | 11:acaefb63fc6b | 89 | addKV(NewCommandMetaData(4, "Remove Schedule")) |
sgnezdov | 11:acaefb63fc6b | 90 | addKV(NewCommandMetaData(5, "Set STM ADC Schedule")) |
sgnezdov | 11:acaefb63fc6b | 91 | addKV(NewCommandMetaData(31, "Close Valve")) |
sgnezdov | 11:acaefb63fc6b | 92 | addKV(NewCommandMetaData(32, "Open Valve")) |
sgnezdov | 11:acaefb63fc6b | 93 | |
sgnezdov | 11:acaefb63fc6b | 94 | Use range 1000 for unique to STM jobs. |
sgnezdov | 11:acaefb63fc6b | 95 | */ |
sgnezdov | 11:acaefb63fc6b | 96 | |
sgnezdov | 11:acaefb63fc6b | 97 | // command #2 is List Schedules in ISOM. |
sgnezdov | 13:0fdbc14c33e7 | 98 | JobSchedulesUpload jSchedulesUpload(conf, lce, scheduler); |
sgnezdov | 11:acaefb63fc6b | 99 | js.Register(2, JobSchedulesUpload::RunAdapter, &jSchedulesUpload); |
sgnezdov | 11:acaefb63fc6b | 100 | |
sgnezdov | 1:eebe442fc126 | 101 | JobTestPrint jtp; |
sgnezdov | 11:acaefb63fc6b | 102 | js.Register(1001, JobTestPrint::RunAdapter, &jtp); |
sgnezdov | 4:b360d4f0bf34 | 103 | |
sgnezdov | 4:b360d4f0bf34 | 104 | JobFakeADC jfa(lce); |
sgnezdov | 11:acaefb63fc6b | 105 | js.Register(1002, JobFakeADC::RunAdapter, &jfa); |
sgnezdov | 0:2c57ed6943f7 | 106 | |
sgnezdov | 1:eebe442fc126 | 107 | scheduler.Start(); |
sgnezdov | 1:eebe442fc126 | 108 | |
sgnezdov | 1:eebe442fc126 | 109 | // inject test case |
sgnezdov | 1:eebe442fc126 | 110 | time_t nowSecs = time(NULL); |
sgnezdov | 7:c4123a87abe2 | 111 | // NOTE: don't schedule run once with at 0, because 0 means never. |
sgnezdov | 1:eebe442fc126 | 112 | JobScheduler::Response<JobScheduler::JobID> res = |
sgnezdov | 13:0fdbc14c33e7 | 113 | //scheduler.JobAdd(1002, new JobScheduler::RunPeriodicSchedule(3, 2), NULL); |
sgnezdov | 13:0fdbc14c33e7 | 114 | scheduler.JobAdd(2, new JobScheduler::RunOnceSchedule(nowSecs+1), NULL); |
sgnezdov | 13:0fdbc14c33e7 | 115 | res = |
sgnezdov | 13:0fdbc14c33e7 | 116 | scheduler.JobAdd(1001, new JobScheduler::RunOnceSchedule(nowSecs+5), NULL); |
sgnezdov | 11:acaefb63fc6b | 117 | //scheduler.JobAdd(1002, new JobScheduler::RunOnceSchedule(1), NULL); |
sgnezdov | 11:acaefb63fc6b | 118 | //scheduler.JobAdd(1001, new JobScheduler::RunOnceSchedule(nowSecs + 2), NULL); |
sgnezdov | 11:acaefb63fc6b | 119 | //res = scheduler.JobAdd(1002, new JobScheduler::RunPeriodicSchedule(2, 5), NULL); |
sgnezdov | 1:eebe442fc126 | 120 | |
sgnezdov | 1:eebe442fc126 | 121 | // block forever unless there is a job that calls scheduler.Stop() |
sgnezdov | 1:eebe442fc126 | 122 | scheduler.WaitToStop(); |
sgnezdov | 1:eebe442fc126 | 123 | |
sgnezdov | 4:b360d4f0bf34 | 124 | /* we can somehow load new application image here and restart */ |
sgnezdov | 4:b360d4f0bf34 | 125 | |
sgnezdov | 1:eebe442fc126 | 126 | // indicate clean app termination |
sgnezdov | 0:2c57ed6943f7 | 127 | tr_info("**Finished**"); //-> "[INFO][main]: this is an info msg" |
sgnezdov | 0:2c57ed6943f7 | 128 | exit(0); |
sgnezdov | 0:2c57ed6943f7 | 129 | } |