Maxim Integrated / Mbed OS MAXREFDES101_SOURCE

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DSInterface.cpp Source File

DSInterface.cpp

00001 /***************************************************************************
00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 ****************************************************************************
00032 */
00033 
00034 #include <ctype.h>
00035 #include <string.h>
00036 #include "DSInterface.h"
00037 
00038 #include "../../Utilities/mxm_assert.h"
00039 #include "../version.h"
00040 #include "Peripherals.h"
00041 #include "utils.h"
00042 #include "Thread.h"
00043 #include "BLE_ICARUS.h"
00044 
00045 
00046 
00047 #define HR_ENABLED     1 << 0
00048 #define SPO2_ENABLED   1 << 1
00049 #define BPT_ENABLED    1 << 2
00050 #define WHRM_ENABLED   1 << 3
00051 #define WSPO2_ENABLED  1 << 4
00052 #define AEC_ENABLED    1 << 5
00053 #define SCD_ENABLED    1 << 6
00054 #define AGC_ENABLED    1 << 7
00055 
00056 
00057 
00058 typedef struct {
00059         uint8_t  fwVersion[3];
00060         uint16_t enAlgoDescBitField;
00061         char me11ProductID;
00062 }fw_type_t;
00063 
00064 const static fw_type_t HUB_FW_TABLE[] = {
00065 
00066         {{1,8,3},   HR_ENABLED   | SPO2_ENABLED  | AGC_ENABLED                             , 'A' },
00067         {{20,1,1},  WHRM_ENABLED | SCD_ENABLED   | AEC_ENABLED                             , 'B' },
00068         {{30,1,1},  WHRM_ENABLED | WSPO2_ENABLED | AGC_ENABLED | SCD_ENABLED | AEC_ENABLED , 'C' },
00069         {{40,0,4},  BPT_ENABLED  | SPO2_ENABLED  | AGC_ENABLED                             , 'D' }
00070 
00071 };
00072 
00073 static const char* ActivAlgoInfo[32] = {
00074         [0]   = "hr,",
00075         [1]   = "spo2,",
00076         [2]   = "bpt,",
00077         [3]   = "whrm,",
00078         [4]   = "wspo2,",
00079         [5]   = "aec,",
00080         [6]   = "scd,",
00081         [7]   = "agc,",
00082 };
00083 
00084 
00085 static const char *glbl_cmds[] = {
00086     "stop",
00087     "get_device_info",
00088     "silent_mode 0",
00089     "silent_mode 1",
00090     "pause 0",
00091     "pause 1",
00092     "enable console",
00093     "disable console",
00094     "set_cfg lcd time",
00095     "set_cfg flash log",
00096     "set_cfg stream ascii", // set streaming mode to ascii
00097     "set_cfg stream bin",   // set streaming mode to binary
00098     "set_cfg report 1",     //set report mode to 1 (brief)
00099     "set_cfg report 2"      // set report mode to 2 (long)
00100 };
00101 
00102 typedef enum {
00103     stop=0,
00104     get_device_info,
00105     silent0_mode,
00106     silent1_mode,
00107     pause0_mode,
00108     pause1_mode,
00109     enable_console,
00110     disable_console,
00111     set_lcd_time,
00112     set_flash_log,
00113     enable_ascii_mode,
00114     enable_bin_mode,
00115     set_cfg_report_1,
00116     set_cfg_report_2,
00117     NUM_CMDS,
00118 } glbl_cmd_state;
00119 
00120 
00121 DSInterface::DSInterface(USBSerial* USB)
00122     :flash_thread_(osPriorityNormal, 2 * 1024)
00123 {
00124     cmd_idx = 0;
00125     silent_mode = false;
00126     pause_mode = false;
00127 
00128     algo_report_mode = 1; // sets the mode for report 1/2
00129     lcd_time_count = 0;
00130     memset(sensor_list, 0, DS_MAX_NUM_SENSORCOMMS * sizeof(SensorComm*));
00131     num_sensors = 0;
00132 
00133     m_USB = USB;
00134 
00135     ds_console_interface_exists_ = false;
00136     recordingStarted = false;
00137 
00138     ds_queue_flash_ = &ds_queue_one_;
00139     ds_queue_fill_  = &ds_queue_two_;
00140     queue_init(ds_queue_flash_, buffer_one_, FLASH_MESSAGE_SIZE, sizeof(buffer_one_));
00141     queue_init(ds_queue_fill_, buffer_two_, FLASH_MESSAGE_SIZE, sizeof(buffer_two_));
00142     flash_thread_.start(callback(&event_queue_, &EventQueue::dispatch_forever));
00143 }
00144 
00145 DSInterface::~DSInterface()
00146 {
00147 }
00148 
00149 void DSInterface::add_sensor_comm(SensorComm *s)
00150 {
00151     mxm_assert_msg(num_sensors < DS_MAX_NUM_SENSORCOMMS, "Too many sensors added to DSInterface. Increase DS_MAX_NUM_SENSORCOMMS.");
00152     sensor_list[num_sensors++] = s;
00153 }
00154 
00155 void DSInterface::enable_console_interface()
00156 {
00157     ds_console_interface_exists_ = true;
00158 }
00159 
00160 void DSInterface::ds_set_ble_status(bool en){
00161     SensorComm *p_sensor;
00162     ds_ble_interface_exists_ = en;
00163 
00164     for(int sensor_num = 0; sensor_num < num_sensors; ++sensor_num) {
00165         p_sensor = sensor_list[sensor_num];
00166         p_sensor->SensorComm_Set_Ble_Status (ds_ble_interface_exists_);
00167     }
00168 }
00169 
00170 void DSInterface::ds_set_flash_rec_status(bool en) {
00171     SensorComm *p_sensor;
00172     recordingStarted = en;
00173     for(int sensor_num = 0; sensor_num < num_sensors; ++sensor_num) {
00174         p_sensor = sensor_list[sensor_num];
00175         p_sensor->SensorComm_Set_Flash_Status(en);
00176     }
00177 }
00178 
00179 void DSInterface::build_command(char ch)
00180 {
00181     if (!this->silent_mode) /* BUG: POTENTIAL BUG, what uart port to echo, not only console */
00182         m_USB->printf("%c", ch);
00183 
00184     if (ch == 0x00) {
00185         pr_err("Ignored char 0x00");
00186         return;
00187     }
00188 
00189     if ((ch == '\n') || (ch == '\r')) {
00190         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00191         cmd_str[cmd_idx++] = '\0';
00192         m_USB->printf("\r\n");
00193         parse_command();
00194 
00195         //Clear cmd_str
00196         while (cmd_idx > 0) /* BUG: POTENTIAL BUG for multiple port access */
00197             cmd_str[--cmd_idx] = '\0';
00198 
00199     } else if ((ch == 0x08 || ch == 0x7F) && cmd_idx > 0) {
00200         //Backspace character
00201         if (cmd_idx > 0)
00202             cmd_str[--cmd_idx] = '\0';
00203     } else {
00204         /* BUG: POTENTIAL BUG for multiple port access */
00205         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00206             cmd_str[cmd_idx++] = ch;
00207     }
00208 
00209 }
00210 
00211 void DSInterface::stopcommand() {
00212     for (int i = 0; i < num_sensors; i++) {
00213         sensor_list[i]->stop();
00214     }
00215     ds_set_flash_rec_status(false);
00216 
00217 }
00218 
00219 void DSInterface::parse_command_str(const char* cmd ) {
00220     strncpy(cmd_str, cmd , strlen(cmd) + 1);
00221     parse_command();
00222 }
00223 
00224 void DSInterface::write_to_flash_thread_funct() {
00225     //static Timer mytimer;
00226     //mytimer.reset();
00227     //mytimer.start();
00228     fwrite((uint8_t*)ds_queue_flash_->base,  1, ds_queue_flash_->item_size * ds_queue_flash_->num_item,  flashFileHandle);
00229     queue_reset(ds_queue_flash_);
00230     //mytimer.stop();
00231     //printf("%f seconds\n", mytimer.read());
00232 }
00233 
00234 void DSInterface::parse_command()
00235 {
00236     int i;
00237     glbl_cmd_state cmd;
00238     char charbuf[512];
00239     char tempbuf[32];
00240     int data_len = 0;
00241     int data_len_log=0;
00242     int ret;
00243     bool parsed_cmd = true;
00244 
00245     printf("%s \n",cmd_str );
00246 
00247     //If top level command, then handle it
00248     for (i = 0; i < NUM_CMDS; i++) {
00249         if (starts_with(&cmd_str[0], glbl_cmds[i])) {
00250             cmd = (glbl_cmd_state)i;
00251 
00252             switch (cmd) {
00253                 case (enable_ascii_mode): {
00254                     sensor_list[0]->AsciiEn = true;
00255                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00256                             "\r\n%s err=0\r\n", cmd_str);
00257                 } break;
00258                 case (enable_bin_mode): {
00259                     sensor_list[0]->AsciiEn = false;
00260                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00261                             "\r\n%s err=0\r\n", cmd_str);
00262                 } break;
00263                 case (stop): {
00264                     for (int i = 0; i < num_sensors; i++) {
00265                         sensor_list[i]->stop();
00266                     }
00267 
00268                     ds_set_flash_rec_status(false);
00269 
00270                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00271                             "\r\n%s err=0\r\n", cmd_str);
00272                 } break;
00273                 case (get_device_info): {
00274 
00275                     data_len = snprintf(charbuf, sizeof(charbuf),
00276                         "\r\n%s platform=%s firmware_ver=%s sensors=", cmd_str, platform_name, FIRMWARE_VERSION);
00277 
00278                     //Add list of sensors
00279                     for (int i = 0; i < num_sensors; i++) {
00280                         if (sensor_list[i]->is_visible()) {
00281                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00282                                             "%s", sensor_list[i]->get_type());
00283                             if (i < (num_sensors - 1))
00284                                 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, ",");
00285                         }
00286                     }
00287 
00288 
00289                     for (int i = 0; i < num_sensors; i++) {
00290                         SensorComm *s = sensor_list[i];
00291                         if (!s->is_visible())
00292                             continue;
00293 
00294                         //Add algo_ver
00295                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00296                                             " algo_ver_%s=%s", s->get_type(), s->get_algo_ver());
00297 
00298                         //Add part name
00299                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00300                                             " part_name_%s=%s", s->get_type(), s->get_part_name());
00301 
00302                         uint8_t part_id, part_rev;
00303                         ret = s->get_part_info(&part_id, &part_rev);
00304 
00305                         if (ret == 0) {
00306                             //Add part id
00307                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00308                                             " part_id_%s=%d", s->get_type(), part_id);
00309 
00310                             //Add rev id
00311                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00312                                             " part_rev_%s=%d", s->get_type(), part_rev);
00313                         }
00314                     }
00315 
00316                     if(firmware_version){
00317                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, " hub_firm_ver=%s", firmware_version);
00318                         if(algo_desc_strsz > 0)
00319                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, "%s", algo_desc_string);
00320                     }
00321 
00322                     data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, " err=0\r\n");
00323 
00324                 } break;
00325                 case (silent0_mode): {
00326                     silent_mode = false;
00327                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00328                             "\r\n%s err=0\r\n", cmd_str);
00329                 } break;
00330                 case (silent1_mode): {
00331                     silent_mode = true;
00332                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00333                             "\r\n%s err=0\r\n", cmd_str);
00334                 } break;
00335                 case (pause0_mode): {
00336                     pause_mode = false;
00337                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00338                             "\r\n%s err=0\r\n", cmd_str);
00339                 } break;
00340                 case (pause1_mode): {
00341                     pause_mode = true;
00342                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00343                             "\r\n%s err=0\r\n", cmd_str);
00344                 } break;
00345                 case (enable_console): {
00346                     ds_console_interface_exists_ = true;
00347                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00348                             "\r\n%s err=0\r\n", cmd_str);
00349                 } break;
00350                 case (disable_console): {
00351                     ds_console_interface_exists_ = false;
00352                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00353                             "\r\n%s err=0\r\n", cmd_str);
00354                 } break;
00355               case (set_lcd_time): {
00356 
00357                 while(1)
00358                 {
00359                     if(cmd_str[17+lcd_time_count] == '\0')
00360                     {
00361                         lcd_time_str[lcd_time_count] = '\0';
00362                         break;
00363                     }
00364                     lcd_time_str[lcd_time_count] = cmd_str[17+lcd_time_count];
00365                     lcd_time_count++;
00366                 }
00367 
00368                 sscanf(lcd_time_str,"%d",&lcd_time_val);
00369 
00370                 set_time(lcd_time_val);
00371 
00372                 lcd_time_count=0;
00373 
00374                 data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00375                         "\r\n%s err=0\r\n", cmd_str);
00376 
00377                 } break;
00378               case (set_flash_log): {
00379 
00380 
00381 
00382                     if(cmd_str[18] == '1' )
00383                     {
00384 
00385 
00386 
00387                         time_t seconds;
00388                         seconds = time(NULL);
00389                         strftime(tempbuf, 32, "/fs/log-%I-%M-%S.maximlog", localtime(&seconds));
00390 
00391                         flashFileHandle = fopen(tempbuf, "w+");
00392 
00393                         if ( flashFileHandle != NULL ) {
00394 
00395                             fprintf(flashFileHandle,"mxim");
00396 
00397                             //1 byte to indicate the version of the log
00398                             unsigned char vers = 0;
00399                             //4 bytes to indicate the number of bytes used to write the format
00400                             unsigned int NumByteBLE = 764;
00401                             unsigned int NumByteNoBLE = 659;
00402 
00403                             fwrite(&vers,sizeof(unsigned char),1,flashFileHandle);
00404 
00405                             if(BLE::Instance().gap().getState().connected){
00406                                 fwrite(&NumByteBLE,sizeof(unsigned int),1,flashFileHandle);
00407                             }
00408                             else
00409                                 fwrite(&NumByteNoBLE,sizeof(unsigned int),1,flashFileHandle);
00410 
00411                             ds_set_flash_rec_status(true);
00412 
00413                             //Write the formats
00414                             data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) - data_len_log- 1,"ppg 0 cs=1 format={smpleCnt,16},"
00415                                     "{grnCnt,20},{grn2Cnt,20},{accelX,14,3},{accelY,14,3},"
00416                                     "{accelZ,14,3},{hr,12},{hrconf,8},{r,11,1},{activity,8}\n"
00417                                     "ppg 1 cs=1 format={smpleCnt,8},{irCnt,20},{redCnt,20},{accelX,14,3},"
00418                                     "{accelY,14,3},{accelZ,14,3},{r,12,1},{wspo2conf,8},"
00419                                     "{spo2,11,1},{wspo2percentcomplete,8},{wspo2lowSNR,1},{wspo2motion,1},{status,8}\n"
00420                                     "bpt 0 cs=1 format={status,4},{irCnt,19},{hr,9},"
00421                                     "{prog,9},{sys_bp,9},{dia_bp,9}\n"
00422                                     "bpt 1 cs=1 format={status,4},{irCnt,19},{hr,9},"
00423                                     "{prog,9},{sys_bp,9},{dia_bp,9}\n");
00424                             if(BLE::Instance().gap().getState().connected){
00425                                 data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) -data_len_log- 1,"ecg 1 cs=1 format={smpleCnt,8},{rtor,14},{rtorbpm,8},"
00426                                         "{pTag.0,3},{eTag.0,3},{ecg.0,18},{pTag.1,3},{eTag.1,3},{ecg.1,18},"
00427                                         "{pTag.2,3},{eTag.2,3},{ecg.2,18},{pTag.3,3},{eTag.3,3},{ecg.3,18}\n");
00428                             }else{
00429                                 data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) -data_len_log- 1,"ecg 1 cs=1 format={smpleCnt,8},{pTag,3},{eTag,3},"
00430                                         "{ecg,18},{rtor,14},{rtorbpm,8}\n");
00431                             }
00432                             data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf)-data_len_log - 1,"ecg 2 cs=1 format={smplCnt,8},{rtor,15}\n");
00433                             data_len_log += snprintf(logbuf+data_len_log, sizeof(logbuf) -data_len_log-1,"temp 0 cs=1 format={smpleCnt,8},{temp,16,2}\n");
00434 
00435 
00436                             fprintf(flashFileHandle,logbuf,data_len_log);
00437                         }
00438                         else {
00439                             ds_set_flash_rec_status(false);
00440                         }
00441 
00442                         data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00443                                 "\r\n%s err=0\r\n", cmd_str);
00444 
00445 
00446 
00447                     }
00448 
00449                     else if (cmd_str[18] == '0'){
00450                         ds_set_flash_rec_status(false);
00451                         handle_file_close();
00452                         data_len += snprintf(charbuf, sizeof(charbuf) - 1,"\r\n%s err=0\r\n", cmd_str);
00453                     }
00454                     else{
00455                         //print cmd not recognized string
00456                         data_len += snprintf(charbuf, sizeof(charbuf) - 1,"\r\n%s err=-255\r\n", cmd_str);
00457                     }
00458 
00459                 } break;
00460                 case (set_cfg_report_1): {
00461                     //m_USB->printf("\r\n MODE 1\r\n");
00462                     algo_report_mode = 1;
00463                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00464                             "\r\n%s err=0\r\n", cmd_str);
00465                     
00466                 } break;
00467                 case (set_cfg_report_2): {
00468                     //m_USB->printf("\r\n MODE 2\r\n");
00469                     algo_report_mode = 2;
00470                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00471                             "\r\n%s err=0\r\n", cmd_str);
00472                 } break;
00473                 default:
00474                     parsed_cmd = false;
00475                     break;
00476             }
00477 
00478             if (parsed_cmd) {
00479                 m_USB->printf(charbuf);
00480 
00481                 if (BLE::Instance().gap().getState().connected) {
00482                     BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00483                 }
00484 
00485             }
00486 
00487             return;
00488         }
00489     }
00490 
00491     //Set the stream type to check the data streaming type later
00492     if(starts_with(&cmd_str[0],"read ppg 0")){
00493         stream_type=0x00;
00494     }
00495     else if(starts_with(&cmd_str[0],"read ppg 1")){
00496         stream_type=0x01;
00497     }
00498     else if(starts_with(&cmd_str[0],"read bpt 0")){
00499         stream_type=0x02;
00500     }
00501     else if(starts_with(&cmd_str[0],"read bpt 1")){
00502         stream_type=0x03;
00503     }
00504     else if(starts_with(&cmd_str[0],"read ecg 1")){
00505         stream_type=0x04;
00506     }
00507     else if(starts_with(&cmd_str[0],"read ecg 2")){
00508         stream_type=0x05;
00509     }
00510     else if(starts_with(&cmd_str[0],"read temp 0")){
00511         stream_type=0x06;
00512     }
00513 
00514     //Loop through each sensor in sensorList
00515     //If sensor.get_type() is contained in cmd_str, pass cmd_str to that sensor's parser
00516     for (int i = 0; i < num_sensors; i++) {
00517         if (strstr(&cmd_str[0], sensor_list[i]->get_type())) {
00518             if (sensor_list[i]->parse_command(cmd_str))
00519                 return;
00520 
00521             break;
00522         }
00523     }
00524 
00525     //If we still haven't found a way to parse the command,
00526     //send it to every sensor until one handles it
00527     for (int i = 0; i < num_sensors; i++) {
00528         if (sensor_list[i]->parse_command(cmd_str))
00529             return;
00530     }
00531     
00532 
00533     //No one could handle the command, print cmd not recognized string
00534     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00535             "\r\n%s err=-255\r\n", cmd_str);
00536     m_USB->printf(charbuf);
00537 
00538     if (BLE::Instance().gap().getState().connected) {
00539         BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00540     }
00541 
00542 }
00543 
00544 void  DSInterface::swap_queues() {
00545     if(flash_thread_.get_state() != Thread::WaitingEventFlag){
00546         printf("overflow happened state %d\r\n", flash_thread_.get_state());
00547         return;
00548     }
00549     queue_t * temp_queue = ds_queue_flash_;
00550     ds_queue_flash_ = ds_queue_fill_;
00551     ds_queue_fill_ = temp_queue;
00552     event_queue_.call(Callback<void()>(this, &DSInterface::write_to_flash_thread_funct));
00553 }
00554 
00555 void DSInterface::handle_file_close() {
00556     //if recording is enabled do not close the file
00557     if(recordingStarted) {
00558         return;
00559     }
00560     if(flashFileHandle != NULL) {
00561         if(flash_thread_.get_state() != Thread::WaitingEventFlag)
00562             return;
00563 
00564         if(ds_queue_fill_->num_item){
00565             swap_queues();
00566             return;
00567         }
00568         printf("closing file\r\n");
00569         fclose(flashFileHandle);
00570         flashFileHandle = NULL;
00571     }
00572 }
00573 
00574 
00575 void DSInterface::force_file_close() {
00576     if(flashFileHandle != NULL) {
00577         while(flash_thread_.get_state() != Thread::WaitingEventFlag) {
00578             wait_ms(1);
00579         }
00580 
00581         if(ds_queue_fill_->num_item){
00582             swap_queues();
00583         }
00584 
00585         while(flash_thread_.get_state() != Thread::WaitingEventFlag) {
00586             wait_ms(1);
00587         }
00588 
00589         //printf("closing file\r\n");
00590         fclose(flashFileHandle);
00591         flashFileHandle = NULL;
00592     }
00593 }
00594 
00595 #define ECGSENSOR_ID 0x02                   //TODO:FIX THESE
00596 #define ECGSENSOR_DATA_REPORT_MODE 0x04     //TODO:FIX THESE
00597 void DSInterface::data_report_execute() {
00598     char buffer[256];
00599     int data_len = 0;
00600     buffer[0] = '\0';
00601     int ret;
00602     bool all_sensors_disabled = true;
00603 
00604     for (int i = 0; i < num_sensors; i++) {
00605         //if ((*it)->is_enabled()) {
00606 
00607         if(!recordingStarted)
00608             data_len = sensor_list[i]->data_report_execute(buffer, sizeof(buffer));
00609         else
00610             data_len = sensor_list[i]->data_report_execute(buffer + FLASH_MESSAGE_OFFSET
00611             , sizeof(buffer) - FLASH_MESSAGE_OFFSET);
00612 
00613         if (!this->pause_mode && (data_len > 0)) {
00614             all_sensors_disabled = false;
00615 
00616             if ((flashFileHandle != NULL) && (recordingStarted)) {
00617                 if(data_len + FLASH_MESSAGE_OFFSET != ds_queue_flash_->item_size) {
00618                     printf("Reinit item size: %d\r\n", data_len);
00619                     queue_update_items_size(ds_queue_flash_, (data_len+ FLASH_MESSAGE_OFFSET));
00620                     queue_update_items_size(ds_queue_fill_, (data_len+ FLASH_MESSAGE_OFFSET));
00621                 }
00622                 //set stream type
00623                 buffer[0] = stream_type;
00624 
00625                 // enqueue data
00626                 ret = enqueue(ds_queue_fill_, buffer);
00627                 if(ret != 0) {
00628                     printf("enqueue has failed %d\r\n", ds_queue_fill_->num_item);
00629                 }
00630                 if(queue_is_full(ds_queue_fill_)) {
00631                     swap_queues();
00632                 }
00633             }
00634 
00635 
00636             if (BLE::Instance().gap().getState().connected & !recordingStarted) {
00637                 ds_console_interface_exists_ = false;
00638                 BLE_Icarus_AddtoQueue((uint8_t *)buffer, (int32_t)sizeof(buffer), data_len);
00639             }
00640 
00641             if (ds_console_interface_exists_ & !recordingStarted){
00642                 if(sensor_list[0]->AsciiEn){
00643                     m_USB->printf(buffer);
00644                 }
00645                 else{
00646                     m_USB->writeBlock((uint8_t*)buffer, data_len);
00647                 }
00648             }
00649             data_len = 0;
00650         }
00651     }
00652 
00653     if(all_sensors_disabled)
00654         handle_file_close();
00655 }
00656 
00657 void DSInterface::set_fw_version(const char* version)
00658 {
00659     if (version && *version){
00660         firmware_version = version;
00661         algo_desc_strsz = get_fw_algorithms();
00662     }
00663 }
00664 
00665 int DSInterface::get_fw_algorithms( void )
00666 {
00667 
00668     int i, fwToReport;
00669     char *start, *end;
00670     uint8_t fwIdx[3];
00671 
00672     start = (char*) firmware_version;
00673 
00674     /*MYG: no need to optimize ,lib function call, just called once at start of execution*/
00675 
00676     fwIdx[0] = (uint8_t) strtol(start,&end, 10)  ; start = end;
00677     fwIdx[1] = (uint8_t) strtol(start+1,&end, 10); start = end;
00678     fwIdx[2] = (uint8_t) strtol(start+1,&end, 10);
00679 
00680     fwToReport = -1;
00681     for( i = 0 ; i < sizeof(HUB_FW_TABLE)/ sizeof(fw_type_t); i++ ){
00682 
00683         if( HUB_FW_TABLE[i].fwVersion[0] == fwIdx[0]/* &
00684             HUB_FW_TABLE[i].fwVersion[1] == fwIdx[1] &
00685             HUB_FW_TABLE[i].fwVersion[2] == fwIdx[2] */) {
00686 
00687                fwToReport = i;
00688                break;
00689             }
00690      }
00691 
00692     if(fwToReport == -1){
00693         return -1;
00694     }
00695 
00696      i = ((fwToReport == -1)? 0:1) * ( /*32 bit*/ 32 - __builtin_clz(HUB_FW_TABLE[fwToReport].enAlgoDescBitField));
00697 
00698      strcpy(&algo_desc_string[0], " fw_algos=");
00699      int tail = strlen(" fw_algos=");
00700 
00701      int descStrSz      = sizeof(algo_desc_string);
00702      while(--i && tail < descStrSz) {
00703 
00704          if(  (HUB_FW_TABLE[fwToReport].enAlgoDescBitField >> i) & 0x0001) {
00705                strcpy( &algo_desc_string[tail], ActivAlgoInfo[i]);
00706                tail += strlen(ActivAlgoInfo[i]);
00707          }
00708 
00709      }
00710      strcpy( &algo_desc_string[tail-1]," ");
00711 
00712      return tail;
00713      //m_USB->printf("%d %s \r\n", tail , algo_desc_string);
00714 
00715 }
00716 
00717 void DSInterface::set_fw_platform(const char* platform)
00718 {
00719     if (platform && *platform)
00720         platform_name = platform;
00721 }