[go: up one dir, main page]

Menu

[34d7f3]: / src / schedule.c  Maximize  Restore  History

Download this file

132 lines (118 with data), 2.8 kB

  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
// SPDX-License-Identifier: GPL-2.0+
/*
* Conversion routines for schedules
*
* Copyright (c) 2020 Heinrich Schuchardt
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "sispm_ctl.h"
#define PMS2_BUFFER_SIZE 0x28
static unsigned char
*pms2_write_block(uint8_t action, uint32_t time, unsigned char *ptr)
{
int i;
*ptr++ = action;
for (i = 0; i < 4; ++i) {
*ptr++ = (uint8_t)time;
time >>= 8;
}
return ptr;
}
/**
* pms2_read_block() - read a single action for EG-PMS2
*
* @action: action to be taken
* @time: time when to take the action
* @ptr: pointer to current action
* Return: pointer to next action
*/
static const unsigned char
*pms2_read_block(uint8_t *action, uint32_t *time, const unsigned char *ptr)
{
int i;
*action = *ptr++;
*time = 0;
for (i = 0; i < 4; ++i) {
*time >>= 8;
*time += (uint32_t)*ptr++ << 24;
}
return ptr;
}
/**
* pms2_schedule_to_buffer() - convert schedule to device buffer
*
* @schedule: schedule
* @buffer: 40 character buffer
*
* Return: 0 = success
*/
int pms2_schedule_to_buffer(const struct plannif *schedule,
unsigned char *buffer)
{
unsigned char *ptr = buffer;
uint32_t loop_ref, start = (uint32_t)schedule->timeStamp;
unsigned char action;
int i;
memset(buffer, 0, PMS2_BUFFER_SIZE);
ptr = pms2_write_block(3 * schedule->socket + 1, start, ptr);
for (i = 0; i < 7; ++i) {
action = schedule->actions[i + 1].switchOn;
start += 60 * schedule->actions[i].timeForNext;
if (!i)
loop_ref = start;
if (action > 1)
break;
ptr = pms2_write_block(action, start, ptr);
}
if (action <= 1) {
fprintf(stderr, "Schedule has too many items\n");
return -1;
}
if (schedule->actions[i].timeForNext)
start -= loop_ref;
else
start = 0;
pms2_write_block(0xe5, start, ptr);
if (start) {
/* set loop flag */
for (ptr -= 5; ptr > buffer; ptr -= 5)
*ptr |= 2;
}
return 0;
}
/**
* pms2_buffer_to_schedule() - device buffer to schedule
*
* @buffer: 40 character buffer
* @schedule: schedule
*/
void pms2_buffer_to_schedule(const unsigned char *buffer,
struct plannif *schedule)
{
const unsigned char *ptr = buffer;
uint32_t start, last, loop_ref, time;
unsigned char action;
int i;
plannif_reset(schedule);
ptr = pms2_read_block(&action, &start, ptr);
schedule->actions[0].switchOn = 0;
schedule->socket = (action - 1) / 3;
schedule->timeStamp = start;
last = start;
for (i = 0; i < 7; ++i) {
ptr = pms2_read_block(&action, &time, ptr);
if (!i)
loop_ref = time;
if (action > 3)
break;
schedule->actions[i + 1].switchOn = action & 1;
schedule->actions[i].timeForNext =
((int32_t)time - (int32_t)last) / 60;
last = time;
}
if (time)
schedule->actions[i].timeForNext =
((int32_t)loop_ref + time - (int32_t)last) / 60;
}