summaryrefslogtreecommitdiff
path: root/src/libs6/ftrigr_update.c
blob: 68e3bde80031e5245b6b254b0450f17424d36d40 (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
/* ISC license. */

#include <stdint.h>
#include <errno.h>
#include <skalibs/error.h>
#include <skalibs/uint16.h>
#include <skalibs/genalloc.h>
#include <skalibs/gensetdyn.h>
#include <skalibs/unixmessage.h>
#include <skalibs/skaclient.h>
#include <s6/ftrigr.h>

static int msghandler (unixmessage_t const *m, void *context)
{
  ftrigr_t *a = (ftrigr_t *)context ;
  ftrigr1_t *p ;
  uint16_t id ;
  if (m->len != 4 || m->nfds) return (errno = EPROTO, 0) ;
  uint16_unpack_big(m->s, &id) ;
  p = GENSETDYN_P(ftrigr1_t, &a->data, id) ;
  if (!p) return 1 ;
  if (p->state != FR1STATE_LISTENING) return (errno = EINVAL, 0) ;
  if (!genalloc_readyplus(uint16_t, &a->list, 1)) return 0 ;
  switch (m->s[2])
  {
    case 'd' :
      p->state = FR1STATE_WAITACK ;
      break ;
    case '!' :
      if (p->options & FTRIGR_REPEAT) p->count++ ;
      else p->state = FR1STATE_WAITACKDATA ;
      break ;
    default : return (errno = EPROTO, 0) ;
  }
  p->what = m->s[3] ;
  id++ ; genalloc_append(uint16_t, &a->list, &id) ;
  return 1 ;
}

int ftrigr_update (ftrigr_t *a)
{
  genalloc_setlen(uint16_t, &a->list, 0) ;
  return skaclient_update(&a->connection, &msghandler, a) ;
}