aboutsummaryrefslogtreecommitdiffstats
path: root/thirdparty/prime/apps/prime_base_appemu/app_emu_common.c
blob: 10dd80243843f79267208fc5f76e1d515cae2a29 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * \file
 *
 * \brief Metering Application Emulator for all node types
 *
 * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * \asf_license_stop
 *
 */

/* System includes */
#include <stdio.h>
#include <string.h>

/* CS includes */
#include "app_emu_common.h"

#include "compiler.h"

#define MULTIPLIER      0x015a4e35L
#define INCREMENT       1

static long l_app_emu_seed; /* /< Randomize seed */
static uint32_t ul_hundreds_of_ms = 0;
uint32_t ul_tens_of_ms = 0;

uint32_t ul_time_app_emu;


/** @brief	Build the time stamp in the buffer requested
 *
 *  @param pc_timestamp    Pointer to the current timestamp
 *
 * This function builds the time stamp included in requests as the number
 * of seconds passed since the 00:00 PM.
 **/
void app_emu_build_timestamp(char *pc_timestamp)
{
#ifdef __GNUC__
	sprintf(pc_timestamp, "%06lu", ul_hundreds_of_ms);
#endif
#ifdef __ICCARM__
	sprintf(pc_timestamp, "%06u", ul_hundreds_of_ms);
#endif

	return;
}

/** @brief	Fill a string with data
 *
 *  @param String      Pointer to the buffer to write data
 *  @param Size        Data size to fill
 *  @param DownUp      Boolean to indicate if packet is uplink / downlink
 *  @param Step        Number of step in application
 *  @param TimeStamp   Time stamp with the current instant
 *
 * This function prepares the data for the packet to be sent and fills it
 * with ascii characters
 **/

void app_emu_fill_string(char *pc_str, uint16_t us_size, uint8_t uc_down_up,
		uint16_t us_step, char *pc_timestamp)
{
	uint16_t i;
	char c_asc = 'A';
	char pc_down_up_str[] = {"UP"};

	/* Build the main string to be transmitted */
	if (uc_down_up) {
		sprintf(pc_down_up_str, "UP");
	} else {
		sprintf(pc_down_up_str, "DW");
	}

	sprintf(pc_str, "%s%s%d%s", pc_down_up_str,
			PROVIDER, (uint16_t)us_step, pc_timestamp);

	/* Fill it with ascii characters */
	for (i = (uint16_t)strlen(pc_str); i < us_size; i++) {
		pc_str[i] = c_asc;

		if (c_asc == 'Z') {
			c_asc = 'A';
		} else {
			c_asc++;
		}
	}
	pc_str[us_size] = '\0';
}

/** @brief	Update timers 1 ms
 *
 * This function update application timers
 **/

void app_emu_update_10ms(void)
{
	if (ul_time_app_emu) {
		ul_time_app_emu--;
	}

	if (ul_tens_of_ms++ == 10) {
		ul_hundreds_of_ms++;
	}
}


/** @brief	initializes random number generator
 *
 *       @param	puc_mac		Pointer to the MAC address
 *
 **/

void app_emu_init_random(const uint8_t *puc_mac)
{
	uint8_t i;

	l_app_emu_seed = 0;

	for (i = 0; i < 6; i++) {
		l_app_emu_seed += (uint16_t)puc_mac[i];
	}
	l_app_emu_seed <<= 2;
}

/** @brief	random number generator
 *
 *      @return	the random number
 *
 * rand uses a multiplicative congruential random number generator with period
 * 2^32 to return successive pseudo-
 * random numbers in the range from 0 to 2^15 - 1.
 * The generator is reinitialized by calling srand with an argument value of 1.
 * It can be set to a new starting point by
 * calling srand with a given seed number.
 **/

int app_emu_random(void)
{
	l_app_emu_seed = MULTIPLIER * l_app_emu_seed + INCREMENT;
	return((int)((uint32_t)l_app_emu_seed >> 16) & 0x7fff);
}