/**
 * Functions for the NB-IoT module BC95.
 */ namespace bc95 { let SERVER = null ; let PORT = 9090 ; let APN = null ; let USER = "" ; let PASS = "" ; let ERROR = false ; param tx param rx param rate export function init ( tx , rx , rate ) { modem . init ( tx , rx , rate ) ; modem . setATPrefix ( "\rAT" ) ; let r = modem . sendAT ( "+CGMR" ) ; modem . expectOK ( "+NCONFIG=AUTOCONNECT,TRUE" ) ; modem . expectOK ( "+NCONFIG=CR_0354_0338_SCRAMBLING,TRUE" ) ; modem . expectOK ( "+NCONFIG=CR_0859_SI_AVOID,TRUE" ) ; } param tries param ops export function attach ( ops = 26201 , tries = 6 ) { modem . expectOK ( "+CFUN=1" ) ; if ( APN != null ) { modem . expectOK ( "+CGDCONT=1,\"IP\",\"" + APN + "\"" ) ; } if ( modem . expectOK ( "+COPS=1,2,\"" + ops + "\"" ) ) { for ( let i = 0 ; i < tries ; i ++ ) { if ( modem . sendAT ( "+CGATT?" ) [ 0 ] == "+CGATT:1" ) break ; basic . pause ( 1000 ) ; } } } param host param port export function setServer ( host , port ) { SERVER = host ; PORT = port ; } param apn export function setAPN ( apn ) { APN = apn ; } param message param receivePort export function send ( message , receivePort = 44567 ) { ERROR = true ; if ( message . length > 0 && SERVER != null && SERVER . length > 0 ) { let response = modem . sendAT ( "+NSOCR=DGRAM,17," + receivePort + ",1" ) ; if ( response [ response . length - 1 ] == "OK" ) { let socket = response [ 0 ] ; ERROR = ! modem . expectOK ( "+NSOST=" + socket + "," + SERVER + "," + PORT + "," + ( message . length ) + "," + stringToHex ( message ) ) ; modem . expectOK ( "+NSOCL=" + socket ) ; } } } /**
     * Check if the last send operation was successful.
     * Also reset the status.
     */ export function sendOk ( ) { if ( ERROR ) { ERROR = false ; return false ; } else return true ; } export function numberToHex ( n ) { return stringToHex ( numberToString ( n ) ) ; } export function numberToString ( n ) { return String . fromCharCode ( ( n >> 24 ) & 0xff ) + String . fromCharCode ( ( n >> 16 ) & 0xff ) + String . fromCharCode ( ( n >> 8 ) & 0xff ) + String . fromCharCode ( n & 0xff ) ; } export function stringToHex ( s ) { const l = "0123456789ABCDEF" ; let r = "" ; for ( let i = 0 ; i < s . length ; i ++ ) { let c = s . charCodeAt ( i ) ; r = r + l . substr ( ( c >> 4 ) , 1 ) + l . substr ( ( c & 0x0f ) , 1 ) ; } return r ; } }