forked from sbabic/swupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.c
93 lines (76 loc) · 1.9 KB
/
handler.c
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
/*
* (C) Copyright 2013
* Stefano Babic, DENX Software Engineering, [email protected].
* on behalf of ifm electronic GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include "swupdate.h"
#include "handler.h"
#include "lua_util.h"
#include "util.h"
#define MAX_INSTALLER_HANDLER 64
struct installer_handler supported_types[MAX_INSTALLER_HANDLER];
static unsigned long nr_installers = 0;
static unsigned long handler_index = ULONG_MAX;
int register_handler(const char *desc,
handler installer, HANDLER_MASK mask, void *data)
{
if (nr_installers > MAX_INSTALLER_HANDLER - 1)
return -1;
strlcpy(supported_types[nr_installers].desc, desc,
sizeof(supported_types[nr_installers].desc));
supported_types[nr_installers].installer = installer;
supported_types[nr_installers].data = data;
supported_types[nr_installers].mask = mask;
nr_installers++;
return 0;
}
void print_registered_handlers(void)
{
unsigned int i;
if (!nr_installers)
return;
INFO("Registered handlers:");
for (i = 0; i < nr_installers; i++) {
INFO("\t%s", supported_types[i].desc);
}
}
struct installer_handler *find_handler(struct img_type *img)
{
unsigned int i;
for (i = 0; i < nr_installers; i++) {
if ((strlen(img->type) == strlen(supported_types[i].desc)) &&
strcmp(img->type,
supported_types[i].desc) == 0)
break;
}
if (i >= nr_installers)
return NULL;
return &supported_types[i];
}
struct installer_handler *get_next_handler(void)
{
if (handler_index == ULONG_MAX) {
handler_index = 0;
}
if (handler_index >= nr_installers) {
handler_index = ULONG_MAX;
return NULL;
}
return &supported_types[handler_index++];
}
unsigned int get_handler_mask(struct img_type *img)
{
struct installer_handler *hnd;
unsigned int mask = 0;
hnd = find_handler(img);
if (hnd)
mask = hnd->mask;
return mask;
}