1 /******************************************************************************
2  *
3  *  Copyright (C) 2018 ST Microelectronics S.A.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *
18  ******************************************************************************/
19 #include "Utils.h"
20 
21 #include <stdio.h>
22 #include "android_logmsg.h"
23 
24 /*******************************************************************************
25 **
26 ** Function        Utils_charArrayToHexString
27 **
28 ** Description     Converts the given char array into its HEX-based string
29 **                 representation.
30 **
31 ** Parameters      array     - char array to be converted.
32 **                 length    - length of the char array.
33 **                 hexString - Output hex string buffer.
34 **
35 ** Returns        0 on successful conversion, -1 otherwise.
36 **
37 *******************************************************************************/
Utils_charArrayToHexString(char * array,int length,char * hexString)38 int Utils_charArrayToHexString(char* array, int length, char* hexString) {
39   char* ptr = hexString;
40   int i;
41   for (i = 0; i < length; i++) {
42     ptr += sprintf(ptr, "%02X ", array[i]);
43   }
44   return 0;
45 }
46 
47 /*******************************************************************************
48 **
49 ** Function        Utils_getElapsedTimeInMs
50 **
51 ** Description     Returns the difference of time (in ms) between t1 and t2.
52 **
53 ** Parameters      t1  - initial time.
54 **                 t2  - final time.
55 **
56 ** Returns       The difference t2 - t1 in ms.
57 **
58 *******************************************************************************/
Utils_getElapsedTimeInMs(struct timeval t1,struct timeval t2)59 int Utils_getElapsedTimeInMs(struct timeval t1, struct timeval t2) {
60   return (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000;
61 }
62 
63 /*******************************************************************************
64 **
65 ** Function        Utils_printCurrentTime
66 **
67 ** Description     Prints current time to standard log.
68 **
69 ** Parameters      prefix- The prefix to be printed before the time.
70 **
71 ** Returns         void
72 **
73 *******************************************************************************/
Utils_printCurrentTime(char * prefix)74 void Utils_printCurrentTime(char* prefix) {
75   struct timeval currentTime;
76   gettimeofday(&currentTime, 0);
77   STLOG_HAL_V("SpiTiming:  %s: %ld,%ld", prefix, currentTime.tv_sec,
78               currentTime.tv_usec);
79 }
80