Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DataStore JobScheduler NetworkServices W5500Interface nanopb protocol
source/main.cpp@17:b79ce8109995, 2017-08-03 (annotated)
- Committer:
 - sgnezdov
 - Date:
 - Thu Aug 03 22:44:59 2017 +0000
 - Revision:
 - 17:b79ce8109995
 - Parent:
 - 16:bef1673b199e
 - Child:
 - 19:40f5bcec121e
 
demo stack underflow issue
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 | 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 | 0:2c57ed6943f7 | 49 | int main() | 
| sgnezdov | 0:2c57ed6943f7 | 50 | { | 
| sgnezdov | 12:ec140a26367f | 51 | Serial pc(USBTX, USBRX); | 
| sgnezdov | 12:ec140a26367f | 52 | pc.baud(115200); | 
| sgnezdov | 12:ec140a26367f | 53 | |
| sgnezdov | 0:2c57ed6943f7 | 54 | printf("\n==Borsch==\n"); | 
| sgnezdov | 9:d6a9210bfd41 | 55 | |
| sgnezdov | 9:d6a9210bfd41 | 56 | // if (initTime() == false) { | 
| sgnezdov | 11:acaefb63fc6b | 57 | // printf("Application terminated: initTime has failed\n"); | 
| sgnezdov | 9:d6a9210bfd41 | 58 | // exit(1); | 
| sgnezdov | 9:d6a9210bfd41 | 59 | // } | 
| sgnezdov | 9:d6a9210bfd41 | 60 | |
| sgnezdov | 0:2c57ed6943f7 | 61 | /* Setup tracing */ | 
| sgnezdov | 0:2c57ed6943f7 | 62 | mbed_trace_mutex_wait_function_set( tracingWait ); // only if thread safety is needed | 
| sgnezdov | 0:2c57ed6943f7 | 63 | mbed_trace_mutex_release_function_set( tracingRelease ); // only if thread safety is needed | 
| sgnezdov | 0:2c57ed6943f7 | 64 | mbed_trace_init(); // initialize the trace library | 
| sgnezdov | 0:2c57ed6943f7 | 65 | |
| sgnezdov | 13:0fdbc14c33e7 | 66 | tr_info("**Started**"); //-> "[INFO][main]: this is an info msg" | 
| sgnezdov | 17:b79ce8109995 | 67 | osThreadId_t tid = osThreadGetId(); | 
| sgnezdov | 17:b79ce8109995 | 68 | tr_debug("Main thread ID: 0x%X", tid); | 
| sgnezdov | 13:0fdbc14c33e7 | 69 | |
| sgnezdov | 13:0fdbc14c33e7 | 70 | Config conf("Nucleo1"); | 
| sgnezdov | 2:661c545d718e | 71 | |
| sgnezdov | 9:d6a9210bfd41 | 72 | uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x45, 0x56, 0x67}; | 
| sgnezdov | 2:661c545d718e | 73 | NetworkInterface* nif = initNetworkStack(mac_addr); | 
| sgnezdov | 2:661c545d718e | 74 | if (nif == NULL) { | 
| sgnezdov | 2:661c545d718e | 75 | tr_error("**Terminated**"); | 
| sgnezdov | 2:661c545d718e | 76 | exit(0); | 
| sgnezdov | 2:661c545d718e | 77 | } | 
| sgnezdov | 0:2c57ed6943f7 | 78 | |
| sgnezdov | 16:bef1673b199e | 79 | LceProxy lce(*nif, conf); | 
| sgnezdov | 4:b360d4f0bf34 | 80 | |
| sgnezdov | 1:eebe442fc126 | 81 | JobScheduler::JobService js; | 
| sgnezdov | 1:eebe442fc126 | 82 | JobScheduler::Scheduler scheduler(&js); | 
| sgnezdov | 1:eebe442fc126 | 83 | |
| sgnezdov | 11:acaefb63fc6b | 84 | /* Job Type ID shall match command id defined in ISOM commandTypes.go | 
| sgnezdov | 11:acaefb63fc6b | 85 | While this rule is not strictly necessary it cuts down on code responsible | 
| sgnezdov | 11:acaefb63fc6b | 86 | for mapping between our internal IDs to CommandIDs. | 
| sgnezdov | 11:acaefb63fc6b | 87 | |
| sgnezdov | 11:acaefb63fc6b | 88 | addKV(NewCommandMetaData(1, "NO-OP")) | 
| sgnezdov | 11:acaefb63fc6b | 89 | addKV(NewCommandMetaData(2, "List Schedules")) | 
| sgnezdov | 11:acaefb63fc6b | 90 | addKV(NewCommandMetaData(3, "Add Schedule")) | 
| sgnezdov | 11:acaefb63fc6b | 91 | addKV(NewCommandMetaData(4, "Remove Schedule")) | 
| sgnezdov | 11:acaefb63fc6b | 92 | addKV(NewCommandMetaData(5, "Set STM ADC Schedule")) | 
| sgnezdov | 11:acaefb63fc6b | 93 | addKV(NewCommandMetaData(31, "Close Valve")) | 
| sgnezdov | 11:acaefb63fc6b | 94 | addKV(NewCommandMetaData(32, "Open Valve")) | 
| sgnezdov | 11:acaefb63fc6b | 95 | |
| sgnezdov | 11:acaefb63fc6b | 96 | Use range 1000 for unique to STM jobs. | 
| sgnezdov | 11:acaefb63fc6b | 97 | */ | 
| sgnezdov | 11:acaefb63fc6b | 98 | |
| sgnezdov | 11:acaefb63fc6b | 99 | // command #2 is List Schedules in ISOM. | 
| sgnezdov | 13:0fdbc14c33e7 | 100 | JobSchedulesUpload jSchedulesUpload(conf, lce, scheduler); | 
| sgnezdov | 11:acaefb63fc6b | 101 | js.Register(2, JobSchedulesUpload::RunAdapter, &jSchedulesUpload); | 
| sgnezdov | 11:acaefb63fc6b | 102 | |
| sgnezdov | 16:bef1673b199e | 103 | JobTestPrint jTestPrint; | 
| sgnezdov | 16:bef1673b199e | 104 | js.Register(1001, JobTestPrint::RunAdapter, &jTestPrint); | 
| sgnezdov | 4:b360d4f0bf34 | 105 | |
| sgnezdov | 16:bef1673b199e | 106 | JobFakeADC jFakeADC(lce); | 
| sgnezdov | 16:bef1673b199e | 107 | js.Register(1002, JobFakeADC::RunAdapter, &jFakeADC); | 
| sgnezdov | 16:bef1673b199e | 108 | |
| sgnezdov | 16:bef1673b199e | 109 | JobTestUpload jTestUpload(lce); | 
| sgnezdov | 16:bef1673b199e | 110 | js.Register(1003, JobTestUpload::RunAdapter, &jTestUpload); | 
| sgnezdov | 0:2c57ed6943f7 | 111 | |
| sgnezdov | 1:eebe442fc126 | 112 | scheduler.Start(); | 
| sgnezdov | 1:eebe442fc126 | 113 | |
| sgnezdov | 1:eebe442fc126 | 114 | // inject test case | 
| sgnezdov | 1:eebe442fc126 | 115 | time_t nowSecs = time(NULL); | 
| sgnezdov | 7:c4123a87abe2 | 116 | // NOTE: don't schedule run once with at 0, because 0 means never. | 
| sgnezdov | 16:bef1673b199e | 117 | //JobScheduler::Response<JobScheduler::JobID> res = | 
| sgnezdov | 16:bef1673b199e | 118 | scheduler.JobAdd(1003, new JobScheduler::RunOnceSchedule(nowSecs+1), NULL); | 
| sgnezdov | 17:b79ce8109995 | 119 | scheduler.JobAdd(2, new JobScheduler::RunOnceSchedule(nowSecs+2), NULL); | 
| sgnezdov | 16:bef1673b199e | 120 | scheduler.JobAdd(1001, new JobScheduler::RunOnceSchedule(nowSecs+5), NULL); | 
| sgnezdov | 16:bef1673b199e | 121 | |
| sgnezdov | 11:acaefb63fc6b | 122 | //scheduler.JobAdd(1002, new JobScheduler::RunOnceSchedule(1), NULL); | 
| sgnezdov | 11:acaefb63fc6b | 123 | //scheduler.JobAdd(1001, new JobScheduler::RunOnceSchedule(nowSecs + 2), NULL); | 
| sgnezdov | 11:acaefb63fc6b | 124 | //res = scheduler.JobAdd(1002, new JobScheduler::RunPeriodicSchedule(2, 5), NULL); | 
| sgnezdov | 1:eebe442fc126 | 125 | |
| sgnezdov | 1:eebe442fc126 | 126 | // block forever unless there is a job that calls scheduler.Stop() | 
| sgnezdov | 1:eebe442fc126 | 127 | scheduler.WaitToStop(); | 
| sgnezdov | 1:eebe442fc126 | 128 | |
| sgnezdov | 4:b360d4f0bf34 | 129 | /* we can somehow load new application image here and restart */ | 
| sgnezdov | 4:b360d4f0bf34 | 130 | |
| sgnezdov | 1:eebe442fc126 | 131 | // indicate clean app termination | 
| sgnezdov | 0:2c57ed6943f7 | 132 | tr_info("**Finished**"); //-> "[INFO][main]: this is an info msg" | 
| sgnezdov | 0:2c57ed6943f7 | 133 | exit(0); | 
| sgnezdov | 0:2c57ed6943f7 | 134 | } |