/* ============================================================================ Name : twr-lib-tester.c Author : Digital Logic Description : Tester for twr-comm library Tester for easy SN/ID ============================================================================ */ #include "../lib/include/twr-comm.h" //----------------------------- #include #include #include #include "tools.h" //----------------------------- // OS specific #ifdef __WIN32__ #include #elif __linux__ // extern int usleep (__useconds_t __useconds); # define Sleep(x) usleep((x)*1000) #endif //----------------------------- static int polling = 1; // TRUE // UID for stop polling - exit from application u8 stop_uid[4] = { 0x16, 0x78, 0x58, 0x56 }; //------------------------------------------------------------------ #define UID_STR_LEN_MAX 32 int fCB_OK(c_string sn, u8 uid[], int uid_len, int control_info) { TWR_STATUS e; char uid_str[UID_STR_LEN_MAX + 1]; // +1 null character size_t uid_str_len = UID_STR_LEN_MAX; hex2str(uid, uid_len, uid_str, &uid_str_len); // TODO: test hex2str() result puts(""); puts("Application CB_OK is called ..."); printf("Arrived packet from TWR SN:[%8s] with UID[len:%d]= %s | " "control_info= %d\n\n", sn, uid_len, uid_str, control_info); puts("Application send ACK..."); int r_status = 0; // OK e = TWR_Packet_Ack(sn, uid, uid_len, control_info, r_status); printf("TWR_Packet_Ack() : %s\n", TWR_Status2Str(e)); puts(""); puts(""); //-------------------------------------------------------------- // stop polling on specific UID if (!memcmp(uid, stop_uid, sizeof(stop_uid))) { puts("--> STOP POLLING UID detected <--"); polling = 0; // FALSE; } //-------------------------------------------------------------- return 0; } int fCB_Error(int err_id, const char *err_msg) { printf("ERROR happens: id= %d | msg= %s\n", err_id, err_msg); return 0; } int main(void) { TWR_STATUS e; printf("Tester for TWR-comm library version: "); puts(TWR_GetLibraryVersionStr()); e = TWR_registerCB_Error((void *) fCB_Error); printf("TWR_registerCB_Error(fCB_Error) : %s\n", TWR_Status2Str(e)); e = TWR_registerCB_OK((void *) fCB_OK); printf("TWR_registerCB_OK(fCB_OK) : %s\n", TWR_Status2Str(e)); //--- puts("Wait for SN/UID events..."); puts(""); while (polling) { e = TWR_Manager(); if (e) { printf("TWR_Manager() : %s\n", TWR_Status2Str(e)); break; } Sleep(1); fflush(stdout); } puts("Done."); puts("Tester exit!"); return EXIT_SUCCESS; }