Sfoglia il codice sorgente

Add developer docs

Fredrik Svantesson 6 anni fa
parent
commit
cf55bd7b23
1 ha cambiato i file con 230 aggiunte e 0 eliminazioni
  1. 230
    0
      DOCUMENTATION.md

+ 230
- 0
DOCUMENTATION.md Vedi File

@@ -0,0 +1,230 @@
1
+# Developer Instructions
2
+
3
+This file is aimed at developers of light, or developers who want to implement "drivers" (enumerators) for their own hardware.
4
+
5
+## Coding Standards
6
+
7
+Light is a small project, which helps keep it clean. However we still would like to see a consistent styling throughout the project, as well as in third-party enumerator implementations. The actual source code may not be fully "up to code" yet, but it's getting there.
8
+
9
+We use 4 spaces for indentation. We have an empty line at the top and bottom of each file.
10
+
11
+The following two sources should be clear enough examples of our coding style:
12
+
13
+### Header files 
14
+
15
+```c
16
+    
17
+    #pragma once
18
+    
19
+    #include <stdbool.h>
20
+    #include <stdint.h>
21
+    #include <stdfoo.h> /* foo_type_t */
22
+    
23
+    
24
+    typedef struct _some_struct_t some_struct_t;
25
+    struct _some_struct_t 
26
+    {
27
+        uint64_t    id;
28
+        foo_type_t  my_foo_thing;
29
+        foo_type_t  *my_foo_ptr;
30
+    }
31
+    
32
+    /* Describe what the purpose of this function is, what it does with/to foo_struct, and what it returns. */
33
+    bool do_some_stuff(some_struct_t *foo_struct);
34
+    
35
+```
36
+
37
+### Source files
38
+
39
+The second line of each source file should be the include to the corresponding header file, followed by an empty line.
40
+
41
+Internal/static functions are always prefixed with an underscore (_).
42
+
43
+```c
44
+
45
+#include "some_struct.h"
46
+
47
+static void _increment_one(uint64_t *out_value)
48
+{
49
+    *out_value += 1;
50
+}
51
+
52
+bool do_some_stuff(some_struct_t *foo_struct)
53
+{
54
+    _increment_one(foo_struct->id);
55
+    
56
+    if(foo_struct->id > 33)
57
+    {
58
+        return false;
59
+    }
60
+    
61
+    if(foo_struct->my_foo_ptr != NULL)
62
+    {
63
+        free(foo_struct->my_foo_ptr);
64
+    }
65
+    
66
+    foo_struct->my_foo_ptr = malloc(sizeof(foo_type_t));
67
+    
68
+    return true;
69
+}
70
+
71
+```
72
+
73
+## Implementing an enumerator 
74
+
75
+Implementing your own devices through an enumerator is pretty easy. The required steps are as follows:
76
+
77
+### Step 1
78
+
79
+Create a headerfile and a corresponding sourcefile under the `impl` folder, Call them `foo.h` and `foo.c`. Open up the `sysfs.c` and `sysfs.h` files for reference implementations.
80
+
81
+### Step 2
82
+
83
+In the header, you need to first do a `#pragma once` (obviously), then `#include "light.h"` to get access to some struct declarations, then at the bare minimum declare 6 functions. If you need to store your own data for each device or device-target, you will need to declare structs for these as well.
84
+
85
+```c
86
+
87
+#pragma once 
88
+
89
+#include "light.h"
90
+
91
+// Implementation of the foo enumerator
92
+// Enumerates devices for quacking ducks
93
+
94
+// Device target data 
95
+struct _impl_foo_data_t
96
+{
97
+    int32_t internal_quack_id;
98
+};
99
+
100
+typedef struct _impl_foo_data_t impl_foo_data_t;
101
+
102
+bool impl_foo_init(light_device_enumerator_t *enumerator);
103
+bool impl_foo_free(light_device_enumerator_t *enumerator);
104
+
105
+bool impl_foo_set(light_device_target_t *target, uint64_t in_value);
106
+bool impl_foo_get(light_device_target_t *target, uint64_t *out_value);
107
+bool impl_foo_getmax(light_device_target_t *target, uint64_t *out_value);
108
+bool impl_foo_command(light_device_target_t *target, char const *command_string);
109
+
110
+```
111
+
112
+### Step 3
113
+
114
+In the sourcefile, you need to implement the 6 methods. Make sure to return `true` on success and `false` on failure. If you do not actually implement a function (for example `impl_foo_command`), just return `true`.
115
+
116
+The job of the enumerator is to identify/enumerate a bunch of different devices (or just one, or even zero if it doesnt find any). You are also responsible to create the device targets for them (i.e, the things that you actually write to on the device). You do this by setting the devices and targets up in `impl_foo_init` and to free these in `impl_foo_free`. The enumerator (this code) owns the memory of the devices and targets, and thus is in charge of allocating and freeing it. 
117
+
118
+```c
119
+
120
+#include "impl/foo.h"
121
+
122
+#include "light.h"
123
+#include "helpers.h"
124
+
125
+bool impl_foo_init(light_device_enumerator_t *enumerator)
126
+{
127
+    /* Lets create a single device, with a single target, for simplicity */
128
+    
129
+    /* Allocate a new device */
130
+    light_device_t *new_device = malloc(sizeof(light_device_t));
131
+    
132
+    /* Set the name of the new device to "new_device_name" */
133
+    snprintf(leds_device->name, sizeof(leds_device->name), "%s", "new_device_name");
134
+    
135
+    /* Add the newly created device to the enumerator */
136
+    light_add_enumerator_device(enumerator, new_device);
137
+
138
+    
139
+    
140
+    /* Allocate a new device target for the newly created device */
141
+    light_device_target_t *new_target = malloc(sizeof(light_device_target_t));
142
+    
143
+    /* Set the name of the new target */
144
+    snprintf(new_target->name, sizeof(new_target->name), "%s", "new_target_name");
145
+    
146
+    /* Setup the function bindings for this target, i.e. what functions will run internally when you run different commands on this device target */
147
+    new_target->set_value = impl_foo_set;
148
+    new_target->get_value = impl_foo_get;
149
+    new_target->get_max_value = impl_foo_getmax;
150
+    new_target->custom_command = impl_foo_command;
151
+    
152
+    /* Finally add the target to the device */
153
+    light_add_device_target(new_device, new_target);
154
+    
155
+    
156
+    
157
+    /* Optional: Setup data specific to either a device or a target, or both. */ 
158
+    /* Useful to for example reference an ID in a third-party API or likewise */
159
+    impl_foo_data_t *custom_data = malloc(sizeof(impl_foo_data_t));
160
+    custom_data->internal_quack_id = 333;
161
+    
162
+    /* You can set it to the device itself, or to a target */
163
+    new_device->device_data = custom_data;
164
+    new_target->device_target_data = custom_data;
165
+
166
+    /* Return true because we didnt get any errors! */
167
+    return true;
168
+}
169
+
170
+bool impl_foo_free(light_device_enumerator_t *enumerator)
171
+{
172
+    /* We are responsible to free the memory we allocated in init, so lets do that */
173
+    
174
+    /* Iterate through the devices in the enumerator */
175
+    for(uint64_t d = 0; d < enumerator->num_devices; d++)
176
+    {
177
+        light_device_t *curr_device = enumerator->devices[d];
178
+
179
+        /* Iterate throug hthe targets in the device */
180
+        for(uint64_t t = 0; t < curr_device->num_targets; t++)
181
+        {
182
+            light_device_target_t *curr_target = curr_device->targets[t];
183
+            
184
+            /* If we allocated any target data, free it here */
185
+            if(curr_target->device_target_data != NULL)
186
+            {
187
+                free(curr_target->device_target_data);
188
+            }
189
+            
190
+            free(curr_target);
191
+        }
192
+
193
+        /* If we allocated any device data, free it here */
194
+        if(curr_device->device_data != NULL)
195
+        {
196
+            free(curr_device->device_data);
197
+        }
198
+        
199
+        /* We need to 'dispose' of the device when we are done with it, to free some internal memory. Always do this when you are about to free a device that you allocated. */
200
+        light_dispose_device(curr_device);
201
+        
202
+        /* Free the device */
203
+        free(curr_device);
204
+    }
205
+    
206
+    return true;
207
+}
208
+
209
+/* Implement the other functions to do their thing. Get, Set and GetMax should be self-explanatory. Command is reserved for future use, but basically will allow the user to run custom commands on a target. */
210
+
211
+```
212
+
213
+### Step 4
214
+
215
+Now that you have implemented your enumerator, it is time to inject it to the application itself. You will be able to compile your enumerator into a plugin in the future, but for now, locate the `light_initialize` function inside `light.c`. You will see some calls (perhaps just one call) to `light_create_enumerator` inside of this function. Add one more call to this function to register your enumerator in the application:
216
+
217
+The first argument is the application context, the second is the name that your enumerator will get, and the last two are the init and free functions that we implemented.
218
+
219
+```c
220
+light_create_enumerator(new_ctx, "foo", &impl_foo_init, &impl_foo_free);
221
+```
222
+
223
+Once you do this, you should be able to find your device target when running `light -L`, and it should be called something like `foo/new_device_name/new_target_name` if you followed this guide.
224
+
225
+The only thing left now is to create a pull request so that the rest of the world can share the functionality that you just implemented!
226
+
227
+
228
+## Troubleshooting
229
+
230
+If you run into any issues, feel free to create a new Github issue, even if you are just asking for "support" or likewise.