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.

Committer:
sgnezdov
Date:
Fri Aug 04 19:27:31 2017 +0000
Revision:
21:32cd3bfde206
Parent:
20:5b52a42b9a5d
Child:
22:3af582d8e227
attempted to add main page to documentation

Who changed what in which revision?

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