BTLE demo for MAXWSNENV.

Dependencies:   BLE_API BMP180 Si7020 mbed MaximBLE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CurrentTimeChar.h Source File

CurrentTimeChar.h

00001 /*******************************************************************************
00002  * Copyright (C) 2015 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 #ifndef __BLE_CURRENTTIMECHAR_H__
00035 #define __BLE_CURRENTTIMECHAR_H__
00036 
00037 #include "mbed.h"
00038 #include "Characteristic.h"
00039 
00040 /**
00041  * @class   CurrentTimeChar
00042  * @brief   CurrentTimeChar characteristic class.
00043  * @details Sub class from the characteristic class.
00044  */
00045 
00046 class CurrentTimeChar : public Characteristic
00047 {
00048 public:
00049 
00050     typedef enum {
00051         MON = 1,
00052         TUE = 2,
00053         WED = 3,
00054         THU = 4,
00055         FRI = 5,
00056         SAT = 6,
00057         SUN = 7
00058     } day_of_week_t;
00059 
00060     static const unsigned OFFSET_OF_YEAR = 0;
00061     static const unsigned OFFSET_OF_MONTH = OFFSET_OF_YEAR + 2;
00062     static const unsigned OFFSET_OF_DAY = OFFSET_OF_MONTH + 1;
00063     static const unsigned OFFSET_OF_HOUR = OFFSET_OF_DAY + 1;
00064     static const unsigned OFFSET_OF_MIN = OFFSET_OF_HOUR + 1;
00065     static const unsigned OFFSET_OF_SEC = OFFSET_OF_MIN + 1;
00066     static const unsigned OFFSET_OF_DOW = OFFSET_OF_SEC + 1;
00067     static const unsigned OFFSET_OF_SSEC = OFFSET_OF_DOW + 1;
00068     static const unsigned OFFSET_OF_REASON = OFFSET_OF_SSEC + 1;
00069 
00070     static const unsigned CHAR_BYTES = OFFSET_OF_REASON + 1;
00071 
00072     CurrentTimeChar() : 
00073         gattChar(GattCharacteristic::UUID_CURRENT_TIME_CHAR, bytes, CHAR_BYTES, CHAR_BYTES,  
00074             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | 
00075             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
00076             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)
00077     {
00078         // Default no timestamp and zero for the value
00079         memset(bytes, 0x0, CHAR_BYTES);
00080     }
00081 
00082     ~CurrentTimeChar() {}
00083 
00084     virtual GattCharacteristic *getChar(void)
00085     {
00086         return &gattChar;
00087     }
00088 
00089     virtual uint8_t getNumBytes(void)
00090     {
00091         return CHAR_BYTES;
00092     }
00093 
00094     virtual uint8_t *getBytes(void)
00095     {
00096         return bytes;
00097     }
00098 
00099     // Update the characteristic value
00100     void update(time_t current_time)
00101     {
00102         // Convert time_t to date_time
00103         date_time(current_time, &bytes[OFFSET_OF_YEAR]);
00104 
00105         // Set SSec to 0
00106         bytes[OFFSET_OF_SSEC] = 0;
00107 
00108         // Set day of the week
00109         struct tm ts = *localtime(&current_time);
00110         if(ts.tm_wday == 0) {
00111             bytes[OFFSET_OF_DOW] = 7;
00112         } else {
00113             bytes[OFFSET_OF_DOW] = ts.tm_wday;
00114         }
00115     }
00116 
00117     // Update the characteristic value with the RTC
00118     void update(void)
00119     {
00120         update(time(NULL));
00121     }
00122 
00123     // Get the characteristic value
00124     time_t get_value(void)
00125     {
00126         struct tm ts;
00127         ts.tm_year = (bytes[OFFSET_OF_YEAR + 1] << 8) + 
00128             bytes[OFFSET_OF_YEAR] - 1900;
00129 
00130         ts.tm_mon = bytes[OFFSET_OF_MONTH] - 1;
00131         ts.tm_mday = bytes[OFFSET_OF_DAY];
00132         ts.tm_hour = bytes[OFFSET_OF_HOUR];
00133         ts.tm_min = bytes[OFFSET_OF_MIN];
00134         ts.tm_sec = bytes[OFFSET_OF_SEC];
00135 
00136         return mktime(&ts);
00137     }
00138 
00139     // Update the RTC with the characteristic value
00140     void update_rtc()
00141     {
00142         set_time(this->get_value());
00143     }
00144 
00145 private:
00146     GattCharacteristic      gattChar;
00147     uint8_t                 bytes[CHAR_BYTES];
00148 };
00149 
00150 #endif /* #ifndef __BLE_CURRENTTIMECHAR_H__*/