Created
September 2, 2010 05:13
-
-
Save voidlizard/561903 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "abi.beep" | |
| @literal DEBUG true; | |
| %%@literal DEBUG false; | |
| @literal UDP 0; | |
| @literal TCP 1; | |
| @literal MODEM_UART 0; | |
| @literal MEM_ALLOCATED 0; | |
| @literal MEM_FREE 1; | |
| @literal MEM_MAX_BLOCK 2; | |
| @literal SMSC ""; | |
| @literal APN ""; | |
| @literal APN_LOGIN ""; | |
| @literal APN_PASSW ""; | |
| @literal HOST "localhost"; | |
| @literal PORT "8888"; | |
| %%@literal HOST ""; | |
| %%@literal PORT ""; | |
| @literal ADC_POWER12V 700; | |
| @literal ADC_PWR_TRIES 5; | |
| @literal VERSION "VV:003007;"; | |
| @literal REG_SMS "010000241"; | |
| @literal SIMCARD_ACTIVATE "*101*1111%%"; | |
| @literal PINCODE 0xFFFF; | |
| @literal CODE_RELOAD 0x0C; | |
| @literal REBOOT 0x0D; | |
| @literal RESET 0x0E; | |
| @literal GPS_RESET 0x0F; | |
| @literal ST_UPDATE_TIME 3; | |
| @literal ST_SEND_TIME 60; | |
| @literal ST_IDLE_TIME 300; | |
| @literal ST_IDLE_TIME2 1800; | |
| @literal ST_BLINK_TIME 60; | |
| @literal REG_TIME 900; | |
| @literal ACC_UPDATE_TIME 3600; | |
| @literal DNS_QUERY_TIME 3600; | |
| @literal SMS_CHECK_TIME 20; | |
| @literal ST_HIT 0x01; | |
| @literal ST_PWR 0x02; | |
| @literal ST_GUARD 0x04; | |
| @literal ST_TRACK 0x08; | |
| @literal ST_ANSW 0x40; | |
| @literal ST_FIX 0x80; | |
| @literal ST_RELOAD 0x100; | |
| @literal ST_GPRS_FAIL 0x200; | |
| @literal INITIAL 0; | |
| @literal DIGIT 1; | |
| @literal MAGIC 0x987F; | |
| @literal O_MAGIC 1; %% INT | |
| @literal O_FLAGS 2; %% INT | |
| @literal O_CNUM 3; %% INT | |
| @literal O_RELOAD 4; %% INT | |
| @literal O_SHOCK 5; %% INT | |
| @literal O_GPRS_FAIL 6; %% INT | |
| @literal O_ACCNT 7; %% INT | |
| @literal O_PWR_LVL 8; %% INT | |
| @literal O_COOKIE 12; %% STRING(8) | |
| @literal PWR_LVL_NULL 0xFFFF; | |
| @literal LOG_CHUNK_SIZE 256; | |
| @literal LOG_ENTRY_SIZE 254; | |
| @literal LOG_ENTRIES 32; | |
| @literal COOKIE_CHANGED 0x01; | |
| @literal GUARD_CHANGED 0x02; | |
| @literal SMS_RECEIVED 0x01; | |
| @literal SMS_VALID 0x02; | |
| @literal STATUS_CHANGED 0x04; | |
| @literal SCRIPT_RELOAD 0x08; | |
| @literal NEED_ANSWER 0x10; | |
| @literal NMEA_READ_ATTEMPTS 10; | |
| @literal TEST_CRC 0x00; | |
| @literal TEST_STATUS 0x02; | |
| @literal TEST_SEC 0x04; | |
| @literal TEST_POW_OK 0x06; | |
| @literal TEST_POW 0x08; | |
| @literal TEST_ACCEL 0x0A; | |
| @literal TEST_GPS 0x0C; | |
| @literal TEST_MODEM 0x0E; | |
| @literal TEST_IMEI_LEN 0x10; | |
| @literal TEST_IMEI 0x12; | |
| %% TYPES | |
| type http_header { | |
| http_status:int, | |
| http_content_length:int, | |
| http_content_range_begin:int, | |
| http_content_range_end:int, | |
| http_content_range_total:int | |
| } | |
| type sms_t { | |
| sms_changed:int, | |
| sms_code:int, | |
| sms_cnum:int, | |
| sms_answ:bool, | |
| sms_cookie:string, | |
| sms_flags:int | |
| } | |
| type gps_data { | |
| gps_utc : string | |
| ,gps_sat_view_num : int | |
| ,gps_fix : int | |
| ,gps_gpgga : bool | |
| ,gps_gpgsv : bool | |
| } | |
| type cell_info { | |
| mcc : string, | |
| mnc : string, | |
| cid : string, | |
| lac : string, | |
| sq : string | |
| } | |
| %% MISC | |
| def debugn(s) { | |
| if `DEBUG then putsn(s); | |
| } | |
| def debug(s) { | |
| if `DEBUG then puts(s); | |
| } | |
| %% ACCEL | |
| def lis302dl_init() | |
| { | |
| lis302dl_reg_write(0x20, 0x80|0x47); %% 400 Hz +PD+Zen+Yen+Xen | |
| lis302dl_reg_write(0x21, 0x07); %% FDS+HP FF_WU1 | |
| lis302dl_reg_write(0x22, 0x01); %% I1CFG0 | |
| lis302dl_reg_write(0x32, 11); %% 32 (0.5G) | |
| lis302dl_reg_write(0x33, 12); %% 32 (0.5G) | |
| lis302dl_reg_read(0x23); %% HP_FILTER_RESET | |
| lis302dl_reg_write(0x30, 106); %% LIR+ZHIE+ZLIE+YHIE+YLIE+XHIE+XLIE | |
| sleep_ms(200); | |
| lis302dl_reg_read(0x31); %% PREVENT FROM FALSE FIRING (?) | |
| } | |
| def shock_check() | |
| { | |
| local r31 = lis302dl_reg_read(0x31); | |
| ret r31 & 0x40 != 0; | |
| } | |
| %% MODEM | |
| def modem_setup() | |
| { | |
| watchdog_set(130); | |
| if !modem_power_check() then { | |
| modem_power(true); | |
| while !modem_init(`PINCODE) { | |
| modem_power(false); | |
| sleep_ms(5000); | |
| modem_power(true); | |
| } | |
| } | |
| sleep_ms(5000); | |
| watchdog_set(0); | |
| sleep_ms(1000); | |
| } | |
| def send_sms(s) { | |
| modem_setup(); | |
| watchdog_set(60); | |
| local r = modem_sms_send(`SMSC, s); | |
| watchdog_set(0); | |
| ret r; | |
| } | |
| %% MISC | |
| def str_ntok(s,seps,n:int) | |
| { | |
| local len = strlen(s); | |
| local len2 = vect_len(seps); | |
| local off = 0, size = 0; | |
| if n == 0 then { | |
| off = 0; | |
| size = vect_get(seps,0); | |
| } | |
| elif n >= len2 then { | |
| n = len2; | |
| off = vect_get(seps, len2-1) + 1; | |
| size = len - off + 1; | |
| } | |
| else { | |
| off = vect_get(seps,n-1) + 1; | |
| size = vect_get(seps,n) - off; | |
| } | |
| ret strsub(s, off, size); | |
| } | |
| def blink(times, time_on, time_off) { | |
| while times != 0 { | |
| leds(1); | |
| sleep_ms(time_on); | |
| leds(0); | |
| sleep_ms(time_off); | |
| times = times - 1; | |
| } | |
| } | |
| def collect_gps_data() | |
| { | |
| local satvn = 0xFFFF; | |
| local fix = 0xFFFF; | |
| local utc = "FFFFFF"; | |
| local attempts = `NMEA_READ_ATTEMPTS; | |
| local gpgga = false; | |
| local gpgsv = false; | |
| while attempts > 0 { | |
| local s = nmea_read(); | |
| if startswith(s, "$GPGSV") then { | |
| %%debugn(s); | |
| local sep = strfindall(s,','); | |
| satvn = strtoul(str_ntok(s,sep,3),10); | |
| gpgsv = true; | |
| break; | |
| } | |
| attempts = attempts - 1; | |
| } | |
| attempts = `NMEA_READ_ATTEMPTS; | |
| while attempts > 0 { | |
| local s = nmea_read(); | |
| if startswith(s, "$GPGGA") then { | |
| %%debugn(s); | |
| local sep = strfindall(s,','); | |
| utc = strsub(str_ntok(s,sep,1),0,6); | |
| fix = strtoul(str_ntok(s,sep,6),10); | |
| gpgga = true; | |
| break; | |
| } | |
| attempts = attempts - 1; | |
| } | |
| ret { gps_data: gps_sat_view_num = satvn, gps_fix = fix, gps_utc = utc, | |
| gps_gpgga = gpgga, gps_gpgsv = gpgsv | |
| }; | |
| } | |
| def print_row(c, w) { | |
| while w > 0 { | |
| putc(c); | |
| w = w - 1; | |
| } | |
| } | |
| def print_field(spacer, s, len) { | |
| if strlen(s) < len then | |
| print_row(spacer, len - strlen(s)); | |
| debug(s); | |
| } | |
| def get_power_level_avg() { | |
| local r = 0, i = 0; | |
| while i < 5 { | |
| adc_start_sampling(); | |
| sleep_ms(10); | |
| r = r + (adc_read(0)/5); | |
| i = i + 1; | |
| } | |
| ret r; | |
| } | |
| def main() { | |
| local t1 = seconds(); | |
| leds(1); | |
| dataspace_erase_all(); | |
| dataspace_write_bytes(`TEST_STATUS, "\x00"); %% TESTS STARTED | |
| %% TURN OFF MODEM | |
| while modem_power_check() { | |
| modem_power(false); | |
| sleep_ms(2000); | |
| } | |
| leds(0); | |
| %% TURN ON MODEM | |
| modem_power(true); | |
| blink(3, 200, 200); | |
| %% TURN ON GPS | |
| gps_power(true); | |
| blink(3, 200, 200); | |
| %%QUERY IMEI | |
| local imei = ""; | |
| if modem_power_check() then { | |
| modem_at_command("ATE1",0,0); | |
| while strlen(fgets(`MODEM_UART, 64, 1000)) != 0 {} | |
| modem_at_command("AT+GSN", 0, 0); | |
| local t2 = seconds(); | |
| while true { | |
| imei = fgets(`MODEM_UART, 16, 1000); | |
| local d = strnth(imei, 0) - '0'; | |
| if d >= 0 && d <= 9 then break; | |
| if seconds() - t2 > 20 then break; | |
| } | |
| } | |
| local gps = collect_gps_data(); | |
| leds(0); | |
| local modem_ok = false; | |
| local gps_ok = false; | |
| if strlen(imei) >= 14 then { | |
| modem_ok = true; | |
| dataspace_write_bytes(`TEST_MODEM, "\x01"); | |
| dataspace_write_int(`TEST_IMEI_LEN, strlen(imei)); | |
| dataspace_write_bytes(`TEST_IMEI, imei); | |
| } | |
| if gps.gps_gpgga then { | |
| gps_ok = true; | |
| dataspace_write_bytes(`TEST_GPS, "\x01"); | |
| } | |
| adc_start_sampling(); | |
| sleep_ms(40); | |
| local pw = adc_read(0); | |
| local pw_ok = false; | |
| if pw >= 0x0346 && pw <= 0x03F0 then { | |
| dataspace_write_bytes(`TEST_POW_OK, "\x01"); | |
| pw_ok = true; | |
| putsn("POWER OK"); | |
| } else { | |
| dataspace_write_bytes(`TEST_POW_OK, "\x00"); | |
| } | |
| puts("POWER: "); | |
| putsn(utoa(pw,16)); | |
| dataspace_write_int(`TEST_POW, pw); | |
| %% INIT ACCEL | |
| lis302dl_power_set(true); | |
| lis302dl_init(); | |
| blink(3, 200, 200); | |
| dataspace_write_bytes(`TEST_ACCEL, "\x01"); | |
| dataspace_write_bytes(`TEST_STATUS, "\xAA"); | |
| dataspace_write_int(`TEST_SEC, (seconds() - t1)); %% TESTS FINISHED | |
| local size = 16+strlen(imei); | |
| local crc = str_crc16(dataspace_read_bytes(`TEST_CRC, size)); | |
| dataspace_write_int(`TEST_CRC, crc); | |
| if gps_ok && modem_ok && pw_ok then { | |
| leds(1); | |
| } | |
| while true { sleep_ms(1000); } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment