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