Sigfox Communication library, allowing you to use any kind of UART able Sigfox Transmitter

Dependents:   TestVirtualisation

sigfox.cpp

Committer:
adrien3d
Date:
2016-03-11
Revision:
1:39a0558a3a3a
Parent:
0:996eb84c895e
Child:
2:975b82a3cde0

File content as of revision 1:39a0558a3a3a:

#include "mbed.h"
#include "millis.h"
#include "sigfox.h"

/* sigfox library
 *
 * Copyright (c) Adrien Chapelet for IoTech.fr
 * Apache 2 license
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 */
 
 /*
    This library allows you to use any kind of Sigfox Transmitter, all you have to do is configure pins and baudrate, and check syntax is conform to your datasheet.
 */
 
//---------------------------------------------------------------------------------------------//
//Constructors
Sigfox_::Sigfox_()  {
    Serial device(PIN_RX, PIN_TX);
    _lastSend=-1;
}
/*Sigfox_::Sigfox_(PinMode TX, PinMode RX)  {
    Serial device(p8, p7); 
}*/

//Functions
uint8_t Sigfox_::_nextReturn() {
    Serial device(PIN_RX, PIN_TX);     
    char fstChar = device.getc();
    while(device.getc() != ';');
    return fstChar;
}

void Sigfox_::begin () {
    Serial device(PIN_RX, PIN_TX); 
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)';');
    
    //while(device.available() < 3);

    device.getc(); //'K'
    device.getc(); //'O'
    device.getc(); //';'
}

bool Sigfox_::isReady() {
    Serial device(PIN_RX, PIN_TX);

    unsigned long currentTime = millis();
    if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) {
        return false;
    }
    // Time is ok, ask the modem's status
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc('\r');
    //device.putc((uint8_t)';');

    //return _nextReturn() == OK;
    return true;
/*
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');

    uint8_t response[8] = {0};
    uint8_t i = 0;
    //while(!device.available());
    while(device.peek() != ';') {
        response[i] = device.read();
        while(!device.available());
        ++i;
    }
    response[0]=device.getc(); //'K'
    response[1]=device.getc(); //'O'
    response[2]=device.getc(); //';'
    if ((response[0]=='O') && (response[1]=='K'))
        return OK;*/
}
 
bool Sigfox_::send(const void* data, uint8_t len) {
    uint8_t* bytes = (uint8_t*)data;
    _lastSend = millis();
    
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'S');
    device.putc((uint8_t)'F');
    device.putc((uint8_t)'=');
    
    for(uint8_t i = 0; i < len; ++i) {
        device.putc(bytes[i]);
    }
    device.putc('\r');

    uint8_t ok = _nextReturn();
    if(ok == OK) {
        _nextReturn(); //SENT
        return true;
    }
    return false;
}
 
unsigned long Sigfox_::getID() {
    unsigned long id = 0;
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'I');
    device.putc((uint8_t)'=');
    device.putc((uint8_t)'1');
    device.putc((uint8_t)'0');
    device.putc('\r');
    
    uint8_t response[8] = {0};
    uint8_t i = 0;
    for (i = 0; i<7;++i) {
        response[i] = device.getc();
        ++i;
    }
    /*uint8_t response[8] = {0};
    while(device.getc() != '\0') {
        response[i] = device.getc();
        while(!device.readable());
        ++i;
    }*/
    device.getc(); //';'
    for(uint8_t j = 0; j < i; ++j) {
        id += response[j] << ((i-3-j) * 8);
    }
    return id;
}

unsigned long Sigfox_::getPAC() {
    unsigned long id = 0;
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'I');
    device.putc((uint8_t)'=');
    device.putc((uint8_t)'1');
    device.putc((uint8_t)'1');
    device.putc('\r');
    
    uint8_t response[8] = {0};
    uint8_t i = 0;
    for (i = 0; i<7;++i) {
        response[i] = device.getc();
        ++i;
    }
    
    for(uint8_t j = 0; j < i; ++j) {
        id += response[j] << ((i-3-j) * 16);
    }
    return true;
}

bool Sigfox_::setPowerMode(uint8_t power) {
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'I');
    device.putc((uint8_t)'=');
    device.putc(power);
    device.putc('\r');

    //return _nextReturn() == OK;
    return 1;
}

//------------------------------------------------------------------------------------------------//
//Destructors

/*sigfox::~sf() {
}*/