mm_utils.c 2.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "mm_controller/mm_utils.h"
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <time.h>
#endif

void printBoolEnabled(char *prefix, bool v)
{
    if (v)
    {
        printf("%s: enabled\r\n", prefix);
    }
    else
    {
        printf("%s: disabled\r\n", prefix);
    }
}

int boolAsInt(bool v)
{
    if (v)
        return 1;
    else
        return 0;
}

// Cross platform sleep function
void sleep_ms(int ms)
{
#ifdef WIN32
    Sleep(ms);
#else
    usleep(ms * 1000);
#endif // WIN32
}

// Trim unprintable characters from the string
void trim(char *const a)
{
    char *p = a, *q = a;
    while (isspace(*q))
        ++q;
    while (*q)
        *p++ = *q++;
    *p = '\0';
    while (p > a && isspace(*--p))
        *p = '\0';
}

// Returns device type by hardware type id
MMDeviceType getMMDeviceType(uint8_t deviceType)
{
    if (mmDeviceIsModem(deviceType))
    {
        return modem;
    }
    if (mmDeviceIsBeacon(deviceType))
    {
        return beacon;
    }
    if (mmDeviceIsHedgehog(deviceType))
    {
        return hedgehog;
    }

    return unknown;
}

// Prints version and ID of the device
void printMMDeviceVersionAndId(MarvelmindDeviceVersion *dv)
{
    printf("Version: %d.%02d", (int)dv->fwVerMajor, (int)dv->fwVerMinor);
    printf("%01d", (int)dv->fwVerMinor2);
    //if (dv->fwVerMinor2 != 0) {
    //    printf("%c",(char) (dv->fwVerMinor2+'a' - 1));
    //}
    printf(".%d   CPU ID=%06x", (int)dv->fwVerDeviceType, dv->cpuId);
    printf("\r\n");
}

// Prints device type
void printMMDeviceType(MMDeviceType *dt)
{
    switch (*dt)
    {
    case modem:
    {
        printf("Device is modem \r\n");
        break;
    }
    case beacon:
    {
        printf("Device is beacon \r\n");
        break;
    }
    case hedgehog:
    {
        printf("Device is hedgehog \r\n");
        break;
    }
    default:
    {
        printf("Unknown device type \r\n");
        break;
    }
    }
}

#ifndef WIN32
double getPassedTime(struct timespec *t1, struct timespec *t2)
{
    double t1_fs = t1->tv_nsec / 1000000000.0;
    double t2_fs = t2->tv_nsec / 1000000000.0;

    double dt_sec = t2->tv_sec - t1->tv_sec;

    return (dt_sec) + (t2_fs - t1_fs);
}
#endif