1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include "application.h"
23 #include "hals/common.h"
24 
25 /****************************************************************************/
26 /* Magic constants
27  *
28  * Only Acropora knows these numbers. The AP has to ask.
29  *
30  * It's a pain to create multiple variable-length arrays using strictly correct
31  * C, but the Weaver service is in the Nugget OS repo so we can hard-code the
32  * sizes here. If it ever changes we'll use the hal.version field to distinguish
33  * which one we're using.
34  *
35  * Still, we want to match the AIDL definitions as closely as possible, to
36  * make our code easier to understand and maintain.
37  */
38 #define NOS2_WEAVER_NUM_SLOTS 64
39 #define NOS2_WEAVER_KEY_BYTES (128 / 8)
40 #define NOS2_WEAVER_VALUE_BYTES (128 / 8)
41 static_assert((NOS2_WEAVER_KEY_BYTES & 0x4) == 0,
42               "NOS2_WEAVER_KEY_BYTES is not a multiple of 4");
43 static_assert((NOS2_WEAVER_VALUE_BYTES & 0x4) == 0,
44               "NOS2_WEAVER_VALUE_BYTES is not a multiple of 4");
45 
46 typedef uint8_t nos2_weaver_key_t[NOS2_WEAVER_KEY_BYTES];
47 typedef uint8_t nos2_weaver_value_t[NOS2_WEAVER_VALUE_BYTES];
48 
49 /****************************************************************************/
50 /* The command is sent separately from any data */
51 
52 enum nos2_weaver_cmd {
53   NOS2_WEAVER_GET_CONFIG,
54   NOS2_WEAVER_WRITE,
55   NOS2_WEAVER_READ,
56   NOS2_WEAVER_ERASE_VALUE,
57 
58   NOS2_WEAVER_NUM_CMDS
59 };
60 
61 /****************************************************************************/
62 /* Request/Response data. Both are optional and depend on the command. */
63 
64 /** NOS2_WEAVER_GET_CONFIG */
65 /* There is no struct nos2_weaver_get_config_request */
66 struct nos2_weaver_get_config_response {
67   struct nos2_cmd_hal hal;
68 
69   uint32_t slots;
70   uint32_t key_size;
71   uint32_t value_size;
72 };
73 
74 /** NOS2_WEAVER_WRITE */
75 struct nos2_weaver_write_request {
76   struct nos2_cmd_hal hal;
77 
78   uint32_t slot_id;
79   nos2_weaver_key_t key;
80   nos2_weaver_value_t value;
81 };
82 /* There is no struct nos2_weaver_write_response */
83 
84 /** NOS2_WEAVER_READ */
85 struct nos2_weaver_read_request {
86   struct nos2_cmd_hal hal;
87 
88   uint32_t slot_id;
89   nos2_weaver_key_t key;
90 };
91 
92 enum nos2_weaver_read_status {
93     NOS2_WEAVER_READ_STATUS_OK,
94     NOS2_WEAVER_READ_STATUS_FAILED,
95     NOS2_WEAVER_READ_STATUS_INCORRECT_KEY,
96     NOS2_WEAVER_READ_STATUS_THROTTLE,
97 };
98 
99 struct nos2_weaver_read_response {
100   struct nos2_cmd_hal hal;
101 
102   uint32_t timeout;
103   uint32_t status;  /* enum nos2_weaver_read_status, but of specified size */
104   /* Put potentially variable-length members at the end. It's NOT, though */
105   nos2_weaver_value_t value;
106 };
107 
108 /** NOS2_WEAVER_ERASE_VALUE */
109 struct nos2_weaver_erase_request {
110   struct nos2_cmd_hal hal;
111 
112   uint32_t slot_id;
113 };
114 /* There is no struct nos2_weaver_erase_response */
115 
116 /****************************************************************************/
117 #ifdef __cplusplus
118 }
119 #endif
120