Example #1
0
/*
 * npfctl_sessions_load: import a list of sessions, reconstruct them and load.
 */
int
npfctl_sessions_load(u_long cmd, void *data)
{
	const struct plistref *pref = data;
	npf_sehash_t *sehasht = NULL;
	prop_dictionary_t sesdict, sedict;
	prop_object_iterator_t it;
	prop_array_t selist;
	int error;

	/* Retrieve the dictionary containing session and NAT policy lists. */
	error = prop_dictionary_copyin_ioctl(pref, cmd, &sesdict);
	if (error)
		return error;

	/*
	 * Note: session objects contain the references to the NAT policy
	 * entries.  Therefore, no need to directly access it.
	 */
	selist = prop_dictionary_get(sesdict, "session-list");
	if (prop_object_type(selist) != PROP_TYPE_ARRAY) {
		error = EINVAL;
		goto fail;
	}

	/* Create a session hash table. */
	sehasht = sess_htable_create();
	if (sehasht == NULL) {
		error = ENOMEM;
		goto fail;
	}

	/*
	 * Iterate through and construct each session.
	 */
	error = 0;
	it = prop_array_iterator(selist);
	npf_core_enter();
	while ((sedict = prop_object_iterator_next(it)) != NULL) {
		/* Session - dictionary. */
		if (prop_object_type(sedict) != PROP_TYPE_DICTIONARY) {
			error = EINVAL;
			goto fail;
		}
		/* Construct and insert real session structure. */
		error = npf_session_restore(sehasht, sedict);
		if (error) {
			goto fail;
		}
	}
	npf_core_exit();
	sess_htable_reload(sehasht);
fail:
	prop_object_release(selist);
	if (error && sehasht) {
		/* Destroy session table. */
		sess_htable_destroy(sehasht);
	}
	return error;
}
Example #2
0
static int __noinline
npf_mk_natlist(npf_ruleset_t *nset, prop_array_t natlist,
    prop_dictionary_t errdict)
{
	prop_object_iterator_t it;
	prop_dictionary_t natdict;
	int error;

	/* NAT policies - array. */
	if (prop_object_type(natlist) != PROP_TYPE_ARRAY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	error = 0;
	it = prop_array_iterator(natlist);
	while ((natdict = prop_object_iterator_next(it)) != NULL) {
		npf_rule_t *rl = NULL;
		npf_natpolicy_t *np;

		/* NAT policy - dictionary. */
		if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
			NPF_ERR_DEBUG(errdict);
			error = EINVAL;
			break;
		}

		/*
		 * NAT policies are standard rules, plus additional
		 * information for translation.  Make a rule.
		 */
		error = npf_mk_singlerule(natdict, NULL, &rl, errdict);
		if (error) {
			break;
		}
		npf_ruleset_insert(nset, rl);

		/* If rule is named, it is a group with NAT policies. */
		if (prop_dictionary_get(natdict, "name") &&
		    prop_dictionary_get(natdict, "subrules")) {
			continue;
		}

		/* Allocate a new NAT policy and assign to the rule. */
		np = npf_nat_newpolicy(natdict, nset);
		if (np == NULL) {
			NPF_ERR_DEBUG(errdict);
			error = ENOMEM;
			break;
		}
		npf_rule_setnat(rl, np);
	}
	prop_object_iterator_release(it);
	/*
	 * Note: in a case of error, caller will free entire NAT ruleset
	 * with assigned NAT policies.
	 */
	return error;
}
Example #3
0
static int __noinline
npf_mk_rules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
    prop_dictionary_t errdict)
{
	prop_object_iterator_t it;
	prop_dictionary_t rldict, rpdict;
	int error;

	/* Rule procedures and the ruleset - arrays. */
	if (prop_object_type(rprocs) != PROP_TYPE_ARRAY ||
	    prop_object_type(rules) != PROP_TYPE_ARRAY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	it = prop_array_iterator(rprocs);
	while ((rpdict = prop_object_iterator_next(it)) != NULL) {
		if (prop_dictionary_get(rpdict, "rproc-ptr")) {
			prop_object_iterator_release(it);
			NPF_ERR_DEBUG(errdict);
			return EINVAL;
		}
	}
	prop_object_iterator_release(it);

	error = 0;
	it = prop_array_iterator(rules);
	while ((rldict = prop_object_iterator_next(it)) != NULL) {
		prop_array_t subrules;
		npf_ruleset_t *rlsetsub;
		npf_rule_t *rl;

		/* Generate a single rule. */
		error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
		if (error) {
			break;
		}
		npf_ruleset_insert(rlset, rl);

		/* Check for sub-rules and generate, if any. */
		subrules = prop_dictionary_get(rldict, "subrules");
		if (subrules == NULL) {
			/* No subrules, next.. */
			continue;
		}
		rlsetsub = npf_rule_subset(rl);
		error = npf_mk_subrules(rlsetsub, subrules, rprocs, errdict);
		if (error)
			break;
	}
	prop_object_iterator_release(it);
	/*
	 * Note: in a case of error, caller will free the ruleset.
	 */
	return error;
}
Example #4
0
static npf_rproc_t *
npf_mk_singlerproc(prop_dictionary_t rpdict)
{
	prop_object_iterator_t it;
	prop_dictionary_t extdict;
	prop_array_t extlist;
	npf_rproc_t *rp;

	extlist = prop_dictionary_get(rpdict, "extcalls");
	if (prop_object_type(extlist) != PROP_TYPE_ARRAY) {
		return NULL;
	}

	rp = npf_rproc_create(rpdict);
	if (rp == NULL) {
		return NULL;
	}

	it = prop_array_iterator(extlist);
	while ((extdict = prop_object_iterator_next(it)) != NULL) {
		const char *name;

		if (!prop_dictionary_get_cstring_nocopy(extdict,
		    "name", &name) || npf_ext_construct(name, rp, extdict)) {
			npf_rproc_release(rp);
			rp = NULL;
			break;
		}
	}
	prop_object_iterator_release(it);
	return rp;
}
Example #5
0
static int __noinline
npf_mk_rules(npf_ruleset_t *rlset, prop_array_t rules, npf_rprocset_t *rpset,
    prop_dictionary_t errdict)
{
	prop_object_iterator_t it;
	prop_dictionary_t rldict;
	int error;

	if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	error = 0;
	it = prop_array_iterator(rules);
	while ((rldict = prop_object_iterator_next(it)) != NULL) {
		npf_rule_t *rl = NULL;

		/* Generate a single rule. */
		error = npf_mk_singlerule(rldict, rpset, &rl, errdict);
		if (error) {
			break;
		}
		npf_ruleset_insert(rlset, rl);
	}
	prop_object_iterator_release(it);
	/*
	 * Note: in a case of error, caller will free the ruleset.
	 */
	return error;
}
Example #6
0
static int __noinline
npf_mk_table_entries(npf_table_t *t, prop_array_t entries)
{
	prop_object_iterator_t eit;
	prop_dictionary_t ent;
	int error = 0;

	if (prop_object_type(entries) != PROP_TYPE_ARRAY) {
		return EINVAL;
	}
	eit = prop_array_iterator(entries);
	while ((ent = prop_object_iterator_next(eit)) != NULL) {
		const npf_addr_t *addr;
		npf_netmask_t mask;
		int alen;

		/* Get address and mask.  Add a table entry. */
		prop_object_t obj = prop_dictionary_get(ent, "addr");
		addr = (const npf_addr_t *)prop_data_data_nocopy(obj);
		prop_dictionary_get_uint8(ent, "mask", &mask);
		alen = prop_data_size(obj);

		error = npf_table_insert(t, alen, addr, mask);
		if (error)
			break;
	}
	prop_object_iterator_release(eit);
	return error;
}
Example #7
0
static void
epe_attach(device_t parent, device_t self, void *aux)
{
	struct epe_softc		*sc = device_private(self);
	struct epsoc_attach_args	*sa;
	prop_data_t			 enaddr;

	aprint_normal("\n");
	sa = aux;
	sc->sc_dev = self;
	sc->sc_iot = sa->sa_iot;
	sc->sc_intr = sa->sa_intr;
	sc->sc_dmat = sa->sa_dmat;

	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 
		0, &sc->sc_ioh))
		panic("%s: Cannot map registers", device_xname(self));

	/* Fetch the Ethernet address from property if set. */
	enaddr = prop_dictionary_get(device_properties(self), "mac-address");
	if (enaddr != NULL) {
		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
		memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
		       ETHER_ADDR_LEN);
		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPE_AFP, 0);
		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
					 sc->sc_enaddr, ETHER_ADDR_LEN);
	}

        ep93xx_intr_establish(sc->sc_intr, IPL_NET, epe_intr, sc);
	epe_init(sc);
}
Example #8
0
static int __noinline
npf_mk_singlerule(prop_dictionary_t rldict, npf_rprocset_t *rpset,
    npf_rule_t **rlret, prop_dictionary_t errdict)
{
	npf_rule_t *rl;
	const char *rname;
	prop_object_t obj;
	int p, error = 0;

	/* Rule - dictionary. */
	if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}
	if ((rl = npf_rule_alloc(rldict)) == NULL) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	/* Assign rule procedure, if any. */
	if (prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rname)) {
		npf_rproc_t *rp;

		if (rpset == NULL) {
			error = EINVAL;
			goto err;
		}
		if ((rp = npf_rprocset_lookup(rpset, rname)) == NULL) {
			NPF_ERR_DEBUG(errdict);
			error = EINVAL;
			goto err;
		}
		npf_rule_setrproc(rl, rp);
	}

	/* Filter code (binary data). */
	if ((obj = prop_dictionary_get(rldict, "code")) != NULL) {
		int type;
		size_t len;
		void *code;

		prop_dictionary_get_int32(rldict, "code-type", &type);
		error = npf_mk_code(obj, type, &code, &len, errdict);
		if (error) {
			goto err;
		}
		npf_rule_setcode(rl, type, code, len);
	}

	*rlret = rl;
	return 0;
err:
	npf_rule_free(rl);
	prop_dictionary_get_int32(rldict, "prio", &p); /* XXX */
	prop_dictionary_set_int32(errdict, "id", p);
	return error;
}
Example #9
0
static int __noinline
npf_mk_singlerule(prop_dictionary_t rldict, prop_array_t rps, npf_rule_t **rl,
    prop_dictionary_t errdict)
{
	const char *rnm;
	npf_rproc_t *rp;
	prop_object_t obj;
	size_t nc_size;
	void *nc;
	int p, error;

	/* Rule - dictionary. */
	if (prop_object_type(rldict) != PROP_TYPE_DICTIONARY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	error = 0;
	obj = prop_dictionary_get(rldict, "ncode");
	if (obj) {
		/* N-code (binary data). */
		error = npf_mk_ncode(obj, &nc, &nc_size, errdict);
		if (error) {
			goto err;
		}
	} else {
		/* No n-code. */
		nc = NULL;
		nc_size = 0;
	}

	/* Check for rule procedure. */
	if (rps && prop_dictionary_get_cstring_nocopy(rldict, "rproc", &rnm)) {
		rp = npf_mk_rproc(rps, rnm);
		if (rp == NULL) {
			if (nc) {
				npf_ncode_free(nc, nc_size);	/* XXX */
			}
			NPF_ERR_DEBUG(errdict);
			error = EINVAL;
			goto err;
		}
	} else {
		rp = NULL;
	}

	/* Finally, allocate and return the rule. */
	*rl = npf_rule_alloc(rldict, rp, nc, nc_size);
	KASSERT(*rl != NULL);
	return 0;
err:
	prop_dictionary_get_int32(rldict, "priority", &p); /* XXX */
	prop_dictionary_set_int32(errdict, "id", p);
	return error;
}
Example #10
0
bool
prop_dictionary_get_dict(prop_dictionary_t dict, const char *key, prop_dictionary_t *dp)
{
	prop_object_t o;
	o = prop_dictionary_get(dict, key);
	if (o == NULL || prop_object_type(o) != PROP_TYPE_DICTIONARY)
		return false;
	*dp = o;
	return true;

}
Example #11
0
/*
 * npf_mk_connlist: import a list of connections and load them.
 */
static int __noinline
npf_mk_connlist(prop_array_t conlist, npf_ruleset_t *natlist,
    npf_conndb_t **conndb, prop_dictionary_t errdict)
{
	prop_dictionary_t condict;
	prop_object_iterator_t it;
	npf_conndb_t *cd;
	int error = 0;

	/* Connection list - array */
	if (prop_object_type(conlist) != PROP_TYPE_ARRAY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	/* Create a connection database. */
	cd = npf_conndb_create();
	it = prop_array_iterator(conlist);
	while ((condict = prop_object_iterator_next(it)) != NULL) {
		/* Connection - dictionary. */
		if (prop_object_type(condict) != PROP_TYPE_DICTIONARY) {
			NPF_ERR_DEBUG(errdict);
			error = EINVAL;
			break;
		}
		/* Construct and insert the connection. */
		error = npf_conn_import(cd, condict, natlist);
		if (error) {
			NPF_ERR_DEBUG(errdict);
			break;
		}
	}
	prop_object_iterator_release(it);
	if (error) {
		npf_conn_gc(cd, true, false);
		npf_conndb_destroy(cd);
	} else {
		*conndb = cd;
	}
	return error;
}
Example #12
0
static void
emac_attach(device_t parent, device_t self, void *aux)
{
	struct emac_softc		*sc = device_private(self);
	struct at91bus_attach_args	*sa = aux;
	prop_data_t			enaddr;
	uint32_t			u;

	printf("\n");
	sc->sc_dev = self;
	sc->sc_iot = sa->sa_iot;
	sc->sc_pid = sa->sa_pid;
	sc->sc_dmat = sa->sa_dmat;

	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
		panic("%s: Cannot map registers", device_xname(self));

	/* enable peripheral clock */
	at91_peripheral_clock(sc->sc_pid, 1);

	/* configure emac: */
	EMAC_WRITE(ETH_CTL, 0);			// disable everything
	EMAC_WRITE(ETH_IDR, -1);		// disable interrupts
	EMAC_WRITE(ETH_RBQP, 0);		// clear receive
	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
	EMAC_WRITE(ETH_TCR, 0);			// send nothing
	//(void)EMAC_READ(ETH_ISR);
	u = EMAC_READ(ETH_TSR);
	EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
				  | ETH_TSR_IDLE | ETH_TSR_RLE
				  | ETH_TSR_COL|ETH_TSR_OVR)));
	u = EMAC_READ(ETH_RSR);
	EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));

	/* Fetch the Ethernet address from property if set. */
	enaddr = prop_dictionary_get(device_properties(self), "mac-addr");

	if (enaddr != NULL) {
		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
		memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
		       ETHER_ADDR_LEN);
	} else {
		static const uint8_t hardcoded[ETHER_ADDR_LEN] = {
		  0x00, 0x0d, 0x10, 0x81, 0x0c, 0x94
		};
		memcpy(sc->sc_enaddr, hardcoded, ETHER_ADDR_LEN);
	}

        at91_intr_establish(sc->sc_pid, IPL_NET, INTR_HIGH_LEVEL, emac_intr, sc);
	emac_init(sc);
}
bool
prop_dictionary_get_bool(prop_dictionary_t dict,
			 const char *key,
			 bool *valp)
{
	prop_bool_t b;

	b = prop_dictionary_get(dict, key);
	if (prop_object_type(b) != PROP_TYPE_BOOL)
		return (false);
	
	*valp = prop_bool_true(b);

	return (true);
}
Example #14
0
/*
 * Do operations associated with quotas
 */
int
ufs_quotactl(struct mount *mp, prop_dictionary_t dict)
{
	struct lwp *l = curlwp;

#if !defined(QUOTA) && !defined(QUOTA2)
	(void) mp;
	(void) dict;
	(void) l;
	return (EOPNOTSUPP);
#else
	int  error;
	prop_dictionary_t cmddict;
	prop_array_t commands;
	prop_object_iterator_t iter;

	/* Mark the mount busy, as we're passing it to kauth(9). */
	error = vfs_busy(mp, NULL);
	if (error)
		return (error);

	error = quota_get_cmds(dict, &commands);
	if (error)
		goto out_vfs;
	iter = prop_array_iterator(commands);
	if (iter == NULL) {
		error = ENOMEM;
		goto out_vfs;
	}
		
		
	mutex_enter(&mp->mnt_updating);
	while ((cmddict = prop_object_iterator_next(iter)) != NULL) {
		if (prop_object_type(cmddict) != PROP_TYPE_DICTIONARY)
			continue;
		error = quota_handle_cmd(mp, l, cmddict);
		if (error)
			break;
	}
	prop_object_iterator_release(iter);
	mutex_exit(&mp->mnt_updating);
out_vfs:
	vfs_unbusy(mp, false, NULL);
	return (error);
#endif
}
Example #15
0
const char *
udev_device_get_property_value(struct udev_device *udev_device,
				const char *key)
{
	prop_object_t	po;
	prop_number_t	pn;
	prop_string_t	ps;
	static char buf[128]; /* XXX: might cause trouble */
	const char *str = NULL;

	if (udev_device->dict == NULL)
		return NULL;

	if ((po = prop_dictionary_get(udev_device->dict, key)) == NULL)
		return NULL;

	if (prop_object_type(po) == PROP_TYPE_STRING) {
		ps = po;
		str = __DECONST(char *, prop_string_cstring_nocopy(ps));
	} else if (prop_object_type(po) == PROP_TYPE_NUMBER) {
/* ARGSUSED */
static void
smsh_axi_attach(device_t parent, device_t self, void *aux)
{
	struct lan9118_softc *sc = device_private(self);
	struct axi_attach_args *aa = aux;
	prop_dictionary_t dict = device_properties(self);
	void *ih;

	sc->sc_dev = self;

	/*
	 * Prefer the Ethernet address in device properties.
	 */
	prop_data_t ea = prop_dictionary_get(dict, "mac-address");
	if (ea != NULL) {
		KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
		KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
		memcpy(sc->sc_enaddr, prop_data_data_nocopy(ea),
		    ETHER_ADDR_LEN);
		sc->sc_flags |= LAN9118_FLAGS_NO_EEPROM;
	}
	/* Map i/o space. */
	if (bus_space_map(aa->aa_iot, aa->aa_addr, LAN9118_IOSIZE, 0,
		&sc->sc_ioh))
		panic("smsh_axi_attach: can't map i/o space");
	sc->sc_iot = aa->aa_iot;

	if (lan9118_attach(sc) != 0) {
		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LAN9118_IOSIZE);
		return;
	}
	/* Establish the interrupt handler. */
	ih = intr_establish(aa->aa_irq, IPL_NET, IST_LEVEL,
	    lan9118_intr, sc);
	if (ih == NULL) {
		aprint_error_dev(self,
		    "couldn't establish interrupt handler\n");
		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LAN9118_IOSIZE);
		return;
	}
}
Example #17
0
static int
npf_insert_nat_rule(prop_dictionary_t natdict, prop_dictionary_t errdict) {
	int error;
	npf_natpolicy_t *np;
	npf_rule_t *rl;
	
	printf("npf_insert_nat_rule\n");
	if (prop_object_type(natdict) != PROP_TYPE_DICTIONARY) {
		printf("rossz tipus!\n");
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	/*
	 * NAT policies are standard rules, plus additional
	 * information for translation.  Make a rule.
	 */
	error = npf_mk_singlerule(natdict, NULL, &rl, errdict);
	if (error) {
		printf("hiba a mksinglerule alatt\n");
		return error;
	}

	npf_core_enter();
	printf("most ruleset inserteljuk\n");
	npf_ruleset_insert(npf_core_natset(), rl);

	/* Allocate a new NAT policy and assign to the rule. */
	np = npf_nat_newpolicy(natdict, npf_core_natset());
	if (np == NULL) {
		printf("hiba a newpolicy alatt\n");
		NPF_ERR_DEBUG(errdict);
		return ENOMEM;
	}
	npf_rule_setnat(rl, np);

	npf_core_exit();
	
	return 0;
}
Example #18
0
static int __noinline
npf_mk_subrules(npf_ruleset_t *rlset, prop_array_t rules, prop_array_t rprocs,
    prop_dictionary_t errdict)
{
	prop_object_iterator_t it;
	prop_dictionary_t rldict;
	int error = 0;

	if (prop_object_type(rules) != PROP_TYPE_ARRAY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}
	it = prop_array_iterator(rules);
	while ((rldict = prop_object_iterator_next(it)) != NULL) {
		npf_rule_t *rl;
		error = npf_mk_singlerule(rldict, rprocs, &rl, errdict);
		if (error) {
			break;
		}
		npf_ruleset_insert(rlset, rl);
	}
	prop_object_iterator_release(it);
	return error;
}
void
com_arbus_attach(device_t parent, device_t self, void *aux)
{
	struct com_arbus_softc *arsc = device_private(self);
	struct com_softc *sc = &arsc->sc_com;
	struct arbus_attach_args *aa = aux;
	prop_number_t prop;
	bus_space_handle_t ioh;

	sc->sc_dev = self;

	prop = prop_dictionary_get(device_properties(sc->sc_dev),
	    "frequency");
	if (prop == NULL) {
		aprint_error(": unable to get frequency property\n");
		return;
	}
	KASSERT(prop_object_type(prop) == PROP_TYPE_NUMBER);

	sc->sc_frequency = (int)prop_number_integer_value(prop);

	if (!com_is_console(aa->aa_bst, aa->aa_addr, &ioh)
	    && bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0,
		    &ioh) != 0) {
		aprint_error(": can't map registers\n");
		return;
	}

	COM_INIT_REGS(sc->sc_regs, aa->aa_bst, ioh, aa->aa_addr);
	sc->sc_regs.cr_nports = aa->aa_size;
	com_arbus_initmap(&sc->sc_regs);

	com_attach_subr(sc);

	arbus_intr_establish(aa->aa_cirq, aa->aa_mirq, comintr, sc);
}
Example #20
0
/*
 * Reads the CPU temperature from /sys/class/thermal/thermal_zone%d/temp (or
 * the user provided path) and returns the temperature in degree celcius.
 *
 */
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
    char *outwalk = buffer;
#ifdef THERMAL_ZONE
    const char *walk;
    bool colorful_output = false;
    char *thermal_zone;

    if (path == NULL)
        asprintf(&thermal_zone, THERMAL_ZONE, zone);
    else {
        static glob_t globbuf;
        if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
            die("glob() failed\n");
        if (globbuf.gl_pathc == 0) {
            /* No glob matches, the specified path does not contain a wildcard. */
            asprintf(&thermal_zone, path, zone);
        } else {
            /* glob matched, we take the first match and ignore the others */
            asprintf(&thermal_zone, "%s", globbuf.gl_pathv[0]);
        }
        globfree(&globbuf);
    }

    INSTANCE(thermal_zone);

    for (walk = format; *walk != '\0'; walk++) {
        if (*walk != '%') {
            *(outwalk++) = *walk;
            continue;
        }

        if (BEGINS_WITH(walk + 1, "degrees")) {
#if defined(LINUX)
            static char buf[16];
            long int temp;
            if (!slurp(thermal_zone, buf, sizeof(buf)))
                goto error;
            temp = strtol(buf, NULL, 10);
            if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
                *(outwalk++) = '?';
            else {
                if ((temp / 1000) >= max_threshold) {
                    START_COLOR("color_bad");
                    colorful_output = true;
                }
                outwalk += sprintf(outwalk, "%ld", (temp / 1000));
                if (colorful_output) {
                    END_COLOR;
                    colorful_output = false;
                }
            }
#elif defined(__DragonFly__)
            struct sensor th_sensor;
            size_t th_sensorlen;

            th_sensorlen = sizeof(th_sensor);

            if (sysctlbyname(thermal_zone, &th_sensor, &th_sensorlen, NULL, 0) == -1) {
                perror("sysctlbyname");
                goto error;
            }
            if (MUKTOC(th_sensor.value) >= max_threshold) {
                START_COLOR("color_bad");
                colorful_output = true;
            }
            outwalk += sprintf(outwalk, "%.2f", MUKTOC(th_sensor.value));
            if (colorful_output) {
                END_COLOR;
                colorful_output = false;
            }

#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
            int sysctl_rslt;
            size_t sysctl_size = sizeof(sysctl_rslt);
            if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0))
                goto error;

            if (TZ_AVG(sysctl_rslt) >= max_threshold) {
                START_COLOR("color_bad");
                colorful_output = true;
            }
            outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
            if (colorful_output) {
                END_COLOR;
                colorful_output = false;
            }

#elif defined(__OpenBSD__)
            struct sensordev sensordev;
            struct sensor sensor;
            size_t sdlen, slen;
            int dev, numt, mib[5] = {CTL_HW, HW_SENSORS, 0, 0, 0};

            sdlen = sizeof(sensordev);
            slen = sizeof(sensor);

            for (dev = 0;; dev++) {
                mib[2] = dev;
                if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
                    if (errno == ENXIO)
                        continue;
                    if (errno == ENOENT)
                        break;
                    goto error;
                }
                /* 'path' is the node within the full path (defaults to acpitz0). */
                if (BEGINS_WITH(sensordev.xname, thermal_zone)) {
                    mib[3] = SENSOR_TEMP;
                    /* Limit to temo0, but should retrieve from a full path... */
                    for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
                        mib[4] = numt;
                        if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
                            if (errno != ENOENT) {
                                warn("sysctl");
                                continue;
                            }
                        }
                        if ((int)MUKTOC(sensor.value) >= max_threshold) {
                            START_COLOR("color_bad");
                            colorful_output = true;
                        }

                        outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value));

                        if (colorful_output) {
                            END_COLOR;
                            colorful_output = false;
                        }
                    }
                }
            }
#elif defined(__NetBSD__)
            int fd, rval;
            bool err = false;
            prop_dictionary_t dict;
            prop_array_t array;
            prop_object_iterator_t iter;
            prop_object_iterator_t iter2;
            prop_object_t obj, obj2, obj3;

            fd = open("/dev/sysmon", O_RDONLY);
            if (fd == -1)
                goto error;

            rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
            if (rval == -1) {
                err = true;
                goto error_netbsd1;
            }

            /* No drivers registered? */
            if (prop_dictionary_count(dict) == 0) {
                err = true;
                goto error_netbsd2;
            }

            iter = prop_dictionary_iterator(dict);
            if (iter == NULL) {
                err = true;
                goto error_netbsd2;
            }

            /* iterate over the dictionary returned by the kernel */
            while ((obj = prop_object_iterator_next(iter)) != NULL) {
                /* skip this dict if it's not what we're looking for */
                if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) != strlen(thermal_zone)) ||
                    (strncmp(thermal_zone,
                             prop_dictionary_keysym_cstring_nocopy(obj),
                             strlen(thermal_zone)) != 0))
                    continue;

                array = prop_dictionary_get_keysym(dict, obj);
                if (prop_object_type(array) != PROP_TYPE_ARRAY) {
                    err = true;
                    goto error_netbsd3;
                }

                iter2 = prop_array_iterator(array);
                if (!iter2) {
                    err = true;
                    goto error_netbsd3;
                }

                /* iterate over array of dicts specific to target sensor */
                while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
                    obj3 = prop_dictionary_get(obj2, "cur-value");

                    float temp = MUKTOC(prop_number_integer_value(obj3));
                    if ((int)temp >= max_threshold) {
                        START_COLOR("color_bad");
                        colorful_output = true;
                    }

                    outwalk += sprintf(outwalk, "%.2f", temp);

                    if (colorful_output) {
                        END_COLOR;
                        colorful_output = false;
                    }

                    break;
                }
                prop_object_iterator_release(iter2);
            }
        error_netbsd3:
            prop_object_iterator_release(iter);
        error_netbsd2:
            prop_object_release(dict);
        error_netbsd1:
            close(fd);
            if (err)
                goto error;

#endif

            walk += strlen("degrees");
        }
    }

    free(thermal_zone);

    OUTPUT_FULL_TEXT(buffer);
    return;
error:
    free(thermal_zone);
#endif

    OUTPUT_FULL_TEXT("can't read temp");
    (void)fputs("i3status: Cannot read temperature. Verify that you have a thermal zone in /sys/class/thermal or disable the cpu_temperature module in your i3status config.\n", stderr);
}
Example #21
0
static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, int number, const char *path, const char *format_down) {
    char *outwalk = buffer;

#if defined(LINUX)
    char buf[1024];
    memset(buf, 0, 1024);
    const char *walk, *last;
    bool watt_as_unit = false;
    int voltage = -1;
    char batpath[512];
    sprintf(batpath, path, number);
    INSTANCE(batpath);

    if (!slurp(batpath, buf, sizeof(buf))) {
        OUTPUT_FULL_TEXT(format_down);
        return false;
    }

    for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
        if (*walk == '\n') {
            last = walk + 1;
            continue;
        }

        if (*walk != '=')
            continue;

        if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW=")) {
            watt_as_unit = true;
            batt_info->remaining = atoi(walk + 1);
            batt_info->percentage_remaining = -1;
        } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW=")) {
            watt_as_unit = false;
            batt_info->remaining = atoi(walk + 1);
            batt_info->percentage_remaining = -1;
        } else if (BEGINS_WITH(last, "POWER_SUPPLY_CAPACITY=") && batt_info->remaining == -1) {
            batt_info->percentage_remaining = atoi(walk + 1);
        } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW="))
            batt_info->present_rate = abs(atoi(walk + 1));
        else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW="))
            voltage = abs(atoi(walk + 1));
        /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
         * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
         * unit instead of μAh. We will calculate it as we need it
         * later. */
        else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW="))
            batt_info->present_rate = abs(atoi(walk + 1));
        else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
            batt_info->status = CS_CHARGING;
        else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
            batt_info->status = CS_FULL;
        else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
            batt_info->status = CS_DISCHARGING;
        else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
            batt_info->status = CS_UNKNOWN;
        else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") ||
                 BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN="))
            batt_info->full_design = atoi(walk + 1);
        else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") ||
                 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))
            batt_info->full_last = atoi(walk + 1);
    }

    /* the difference between POWER_SUPPLY_ENERGY_NOW and
     * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
     * given in mWh, the charge in mAh. So calculate every value given in
     * ampere to watt */
    if (!watt_as_unit && voltage >= 0) {
        if (batt_info->present_rate > 0) {
            batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
        }
        if (batt_info->remaining > 0) {
            batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0));
        }
        if (batt_info->full_design > 0) {
            batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0));
        }
        if (batt_info->full_last > 0) {
            batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0));
        }
    }
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
    int state;
    int sysctl_rslt;
    size_t sysctl_size = sizeof(sysctl_rslt);

    if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
        OUTPUT_FULL_TEXT(format_down);
        return false;
    }

    batt_info->percentage_remaining = sysctl_rslt;
    if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
        OUTPUT_FULL_TEXT(format_down);
        return false;
    }

    batt_info->seconds_remaining = sysctl_rslt * 60;
    if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
        OUTPUT_FULL_TEXT(format_down);
        return false;
    }

    state = sysctl_rslt;
    if (state == 0 && batt_info->percentage_remaining == 100)
        batt_info->status = CS_FULL;
    else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info->percentage_remaining < 100)
        batt_info->status = CS_CHARGING;
    else
        batt_info->status = CS_DISCHARGING;
#elif defined(__OpenBSD__)
    /*
	 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
	 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
	 * probing acpi(4) devices.
	 */
    struct apm_power_info apm_info;
    int apm_fd;

    apm_fd = open("/dev/apm", O_RDONLY);
    if (apm_fd < 0) {
        OUTPUT_FULL_TEXT("can't open /dev/apm");
        return false;
    }
    if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
        OUTPUT_FULL_TEXT("can't read power info");

    close(apm_fd);

    /* Don't bother to go further if there's no battery present. */
    if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
        (apm_info.battery_state == APM_BATT_UNKNOWN)) {
        OUTPUT_FULL_TEXT(format_down);
        return false;
    }

    switch (apm_info.ac_state) {
        case APM_AC_OFF:
            batt_info->status = CS_DISCHARGING;
            break;
        case APM_AC_ON:
            batt_info->status = CS_CHARGING;
            break;
        default:
            /* If we don't know what's going on, just assume we're discharging. */
            batt_info->status = CS_DISCHARGING;
            break;
    }

    batt_info->percentage_remaining = apm_info.battery_life;

    /* Can't give a meaningful value for remaining minutes if we're charging. */
    if (batt_info->status != CS_CHARGING) {
        batt_info->seconds_remaining = apm_info.minutes_left * 60;
    }
#elif defined(__NetBSD__)
    /*
     * Using envsys(4) via sysmon(4).
     */
    int fd, rval;
    bool is_found = false;
    char sensor_desc[16];

    prop_dictionary_t dict;
    prop_array_t array;
    prop_object_iterator_t iter;
    prop_object_iterator_t iter2;
    prop_object_t obj, obj2, obj3, obj4, obj5;

    if (number >= 0)
        (void)snprintf(sensor_desc, sizeof(sensor_desc), "acpibat%d", number);

    fd = open("/dev/sysmon", O_RDONLY);
    if (fd < 0) {
        OUTPUT_FULL_TEXT("can't open /dev/sysmon");
        return false;
    }

    rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
    if (rval == -1) {
        close(fd);
        return false;
    }

    if (prop_dictionary_count(dict) == 0) {
        prop_object_release(dict);
        close(fd);
        return false;
    }

    iter = prop_dictionary_iterator(dict);
    if (iter == NULL) {
        prop_object_release(dict);
        close(fd);
    }

    /* iterate over the dictionary returned by the kernel */
    while ((obj = prop_object_iterator_next(iter)) != NULL) {
        /* skip this dict if it's not what we're looking for */
        if (number < 0) {
            /* we want all batteries */
            if (!BEGINS_WITH(prop_dictionary_keysym_cstring_nocopy(obj),
                             "acpibat"))
                continue;
        } else {
            /* we want a specific battery */
            if (strcmp(sensor_desc,
                       prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
                continue;
        }

        is_found = true;

        array = prop_dictionary_get_keysym(dict, obj);
        if (prop_object_type(array) != PROP_TYPE_ARRAY) {
            prop_object_iterator_release(iter);
            prop_object_release(dict);
            close(fd);
            return false;
        }

        iter2 = prop_array_iterator(array);
        if (!iter2) {
            prop_object_iterator_release(iter);
            prop_object_release(dict);
            close(fd);
            return false;
        }

        struct battery_info batt_buf = {
            .full_design = 0,
            .full_last = 0,
            .remaining = 0,
            .present_rate = 0,
            .status = CS_UNKNOWN,
        };
        int voltage = -1;
        bool watt_as_unit = false;

        /* iterate over array of dicts specific to target battery */
        while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
            obj3 = prop_dictionary_get(obj2, "description");

            if (obj3 == NULL)
                continue;

            if (strcmp("charging", prop_string_cstring_nocopy(obj3)) == 0) {
                obj3 = prop_dictionary_get(obj2, "cur-value");

                if (prop_number_integer_value(obj3))
                    batt_buf.status = CS_CHARGING;
                else
                    batt_buf.status = CS_DISCHARGING;
            } else if (strcmp("charge", prop_string_cstring_nocopy(obj3)) == 0) {
                obj3 = prop_dictionary_get(obj2, "cur-value");
                obj4 = prop_dictionary_get(obj2, "max-value");
                obj5 = prop_dictionary_get(obj2, "type");

                batt_buf.remaining = prop_number_integer_value(obj3);
                batt_buf.full_design = prop_number_integer_value(obj4);

                if (strcmp("Ampere hour", prop_string_cstring_nocopy(obj5)) == 0)
                    watt_as_unit = false;
                else
                    watt_as_unit = true;
            } else if (strcmp("discharge rate", prop_string_cstring_nocopy(obj3)) == 0) {
                obj3 = prop_dictionary_get(obj2, "cur-value");
                batt_buf.present_rate = prop_number_integer_value(obj3);
            } else if (strcmp("charge rate", prop_string_cstring_nocopy(obj3)) == 0) {
                obj3 = prop_dictionary_get(obj2, "cur-value");
                batt_info->present_rate = prop_number_integer_value(obj3);
            } else if (strcmp("last full cap", prop_string_cstring_nocopy(obj3)) == 0) {
                obj3 = prop_dictionary_get(obj2, "cur-value");
                batt_buf.full_last = prop_number_integer_value(obj3);
            } else if (strcmp("voltage", prop_string_cstring_nocopy(obj3)) == 0) {
                obj3 = prop_dictionary_get(obj2, "cur-value");
                voltage = prop_number_integer_value(obj3);
            }
        }
        prop_object_iterator_release(iter2);

        if (!watt_as_unit && voltage != -1) {
            batt_buf.present_rate = (((float)voltage / 1000.0) * ((float)batt_buf.present_rate / 1000.0));
            batt_buf.remaining = (((float)voltage / 1000.0) * ((float)batt_buf.remaining / 1000.0));
            batt_buf.full_design = (((float)voltage / 1000.0) * ((float)batt_buf.full_design / 1000.0));
            batt_buf.full_last = (((float)voltage / 1000.0) * ((float)batt_buf.full_last / 1000.0));
        }

        if (batt_buf.remaining == batt_buf.full_design)
            batt_buf.status = CS_FULL;

        add_battery_info(batt_info, &batt_buf);
    }

    prop_object_iterator_release(iter);
    prop_object_release(dict);
    close(fd);

    if (!is_found) {
        OUTPUT_FULL_TEXT(format_down);
        return false;
    }

    batt_info->present_rate = abs(batt_info->present_rate);
#endif

    return true;
}

/*
 * Populate batt_info with aggregate information about all batteries.
 * Returns false on error, and an error message will have been written.
 */
static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) {
#if defined(LINUX)
    char *outwalk = buffer;
    bool is_found = false;

    char *placeholder;
    char *globpath = sstrdup(path);
    if ((placeholder = strstr(path, "%d")) != NULL) {
        char *globplaceholder = globpath + (placeholder - path);
        *globplaceholder = '*';
        strcpy(globplaceholder + 1, placeholder + 2);
    }

    if (!strcmp(globpath, path)) {
        OUTPUT_FULL_TEXT("no '%d' in battery path");
        return false;
    }

    glob_t globbuf;
    if (glob(globpath, 0, NULL, &globbuf) == 0) {
        for (size_t i = 0; i < globbuf.gl_pathc; i++) {
            /* Probe to see if there is such a battery. */
            struct battery_info batt_buf = {
                .full_design = 0,
                .full_last = 0,
                .remaining = 0,
                .present_rate = 0,
                .status = CS_UNKNOWN,
            };
            if (!slurp_battery_info(&batt_buf, json_gen, buffer, i, globbuf.gl_pathv[i], format_down))
                return false;

            is_found = true;
            add_battery_info(batt_info, &batt_buf);
        }
    }
    globfree(&globbuf);
    free(globpath);

    if (!is_found) {
        OUTPUT_FULL_TEXT(format_down);
        return false;
    }

    batt_info->present_rate = abs(batt_info->present_rate);
#else
    /* FreeBSD and OpenBSD only report aggregates. NetBSD always
     * iterates through all batteries, so it's more efficient to
     * aggregate in slurp_battery_info. */
    return slurp_battery_info(batt_info, json_gen, buffer, -1, path, format_down);
#endif

    return true;
}

void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) {
    const char *walk;
    char *outwalk = buffer;
    struct battery_info batt_info = {
        .full_design = -1,
        .full_last = -1,
        .remaining = -1,
        .present_rate = -1,
        .seconds_remaining = -1,
        .percentage_remaining = -1,
        .status = CS_UNKNOWN,
    };
    bool colorful_output = false;

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
    /* These OSes report battery stats in whole percent. */
    integer_battery_capacity = true;
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
    /* These OSes report battery time in minutes. */
    hide_seconds = true;
#endif

    if (number < 0) {
        if (!slurp_all_batteries(&batt_info, json_gen, buffer, path, format_down))
            return;
    } else {
        if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down))
            return;
    }

    // *Choose* a measure of the 'full' battery. It is whichever is better of
    // the battery's (hardware-given) design capacity (batt_info.full_design)
    // and the battery's last known good charge (batt_info.full_last).
    // We prefer the design capacity, but use the last capacity if we don't have it,
    // or if we are asked to (last_full_capacity == true); but similarly we use
    // the design capacity if we don't have the last capacity.
    // If we don't have either then both full_design and full_last <= 0,
    // which implies full <= 0, which bails out on the following line.
    int full = batt_info.full_design;
    if (full <= 0 || (last_full_capacity && batt_info.full_last > 0)) {
        full = batt_info.full_last;
    }
    if (full <= 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) {
        /* We have no physical measurements and no estimates. Nothing
         * much we can report, then. */
        OUTPUT_FULL_TEXT(format_down);
        return;
    }

    if (batt_info.percentage_remaining < 0) {
        batt_info.percentage_remaining = (((float)batt_info.remaining / (float)full) * 100);
        /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
         * charged, even though that’s plainly wrong. For people who chose to see
         * the percentage calculated based on the last full capacity, we clamp the
         * value to 100%, as that makes more sense.
         * See http://bugs.debian.org/785398 */
        if (last_full_capacity && batt_info.percentage_remaining > 100) {
            batt_info.percentage_remaining = 100;
        }
    }

    if (batt_info.seconds_remaining < 0 && batt_info.present_rate > 0 && batt_info.status != CS_FULL) {
        if (batt_info.status == CS_CHARGING)
            batt_info.seconds_remaining = 3600.0 * (full - batt_info.remaining) / batt_info.present_rate;
        else if (batt_info.status == CS_DISCHARGING)
            batt_info.seconds_remaining = 3600.0 * batt_info.remaining / batt_info.present_rate;
        else
            batt_info.seconds_remaining = 0;
    }

    if (batt_info.status == CS_DISCHARGING && low_threshold > 0) {
        if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) {
            START_COLOR("color_bad");
            colorful_output = true;
        } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
            START_COLOR("color_bad");
            colorful_output = true;
        }
    }

#define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT()                   \
    do {                                                       \
        if (outwalk == prevoutwalk) {                          \
            if (outwalk > buffer && isspace((int)outwalk[-1])) \
                outwalk--;                                     \
            else if (isspace((int)*(walk + 1)))                \
                walk++;                                        \
        }                                                      \
    } while (0)

    for (walk = format; *walk != '\0'; walk++) {
        char *prevoutwalk = outwalk;

        if (*walk != '%') {
            *(outwalk++) = *walk;
            continue;
        }

        if (BEGINS_WITH(walk + 1, "status")) {
            const char *statusstr;
            switch (batt_info.status) {
                case CS_CHARGING:
                    statusstr = status_chr;
                    break;
                case CS_DISCHARGING:
                    statusstr = status_bat;
                    break;
                case CS_FULL:
                    statusstr = status_full;
                    break;
                default:
                    statusstr = status_unk;
            }

            outwalk += sprintf(outwalk, "%s", statusstr);
            walk += strlen("status");
        } else if (BEGINS_WITH(walk + 1, "percentage")) {
            if (integer_battery_capacity) {
                outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark);
            } else {
                outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark);
            }
            walk += strlen("percentage");
        } else if (BEGINS_WITH(walk + 1, "remaining")) {
            if (batt_info.seconds_remaining >= 0) {
                int seconds, hours, minutes;

                hours = batt_info.seconds_remaining / 3600;
                seconds = batt_info.seconds_remaining - (hours * 3600);
                minutes = seconds / 60;
                seconds -= (minutes * 60);

                if (hide_seconds)
                    outwalk += sprintf(outwalk, "%02d:%02d",
                                       max(hours, 0), max(minutes, 0));
                else
                    outwalk += sprintf(outwalk, "%02d:%02d:%02d",
                                       max(hours, 0), max(minutes, 0), max(seconds, 0));
            }
            walk += strlen("remaining");
            EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
        } else if (BEGINS_WITH(walk + 1, "emptytime")) {
            if (batt_info.seconds_remaining >= 0) {
                time_t empty_time = time(NULL) + batt_info.seconds_remaining;
                set_timezone(NULL); /* Use local time. */
                struct tm *empty_tm = localtime(&empty_time);

                if (hide_seconds)
                    outwalk += sprintf(outwalk, "%02d:%02d",
                                       max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
                else
                    outwalk += sprintf(outwalk, "%02d:%02d:%02d",
                                       max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
            }
            walk += strlen("emptytime");
            EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
        } else if (BEGINS_WITH(walk + 1, "consumption")) {
            if (batt_info.present_rate >= 0)
                outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6);

            walk += strlen("consumption");
            EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
        }
    }

    if (colorful_output)
        END_COLOR;

    OUTPUT_FULL_TEXT(buffer);
}
Example #22
0
int
xbps_transaction_commit(struct xbps_handle *xhp)
{
    prop_object_t obj;
    prop_object_iterator_t iter;
    size_t i;
    const char *pkgname, *version, *pkgver, *tract;
    int rv = 0;
    bool update, install, sr;

    assert(prop_object_type(xhp->transd) == PROP_TYPE_DICTIONARY);

    update = install = false;
    iter = xbps_array_iter_from_dict(xhp->transd, "packages");
    if (iter == NULL)
        return EINVAL;
    /*
     * Download binary packages (if they come from a remote repository).
     */
    xbps_set_cb_state(xhp, XBPS_STATE_TRANS_DOWNLOAD, 0, NULL, NULL, NULL);
    if ((rv = download_binpkgs(xhp, iter)) != 0)
        goto out;
    /*
     * Check SHA256 hashes for binary packages in transaction.
     */
    xbps_set_cb_state(xhp, XBPS_STATE_TRANS_VERIFY, 0, NULL, NULL, NULL);
    if ((rv = check_binpkgs_hash(xhp, iter)) != 0)
        goto out;
    /*
     * Install, update, configure or remove packages as specified
     * in the transaction dictionary.
     */
    xbps_set_cb_state(xhp, XBPS_STATE_TRANS_RUN, 0, NULL, NULL, NULL);

    i = 0;
    while ((obj = prop_object_iterator_next(iter)) != NULL) {
        if ((xhp->transaction_frequency_flush > 0) &&
                (++i >= xhp->transaction_frequency_flush)) {
            rv = xbps_pkgdb_update(xhp, true);
            if (rv != 0 && rv != ENOENT)
                goto out;

            i = 0;
        }
        update = false;
        prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
        prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
        prop_dictionary_get_cstring_nocopy(obj, "version", &version);
        prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);

        if (strcmp(tract, "remove") == 0) {
            update = false;
            sr = false;
            /*
             * Remove package.
             */
            prop_dictionary_get_bool(obj, "remove-and-update",
                                     &update);
            prop_dictionary_get_bool(obj, "softreplace", &sr);
            rv = xbps_remove_pkg(xhp, pkgname, version, update, sr);
            if (rv != 0)
                goto out;
        } else if (strcmp(tract, "configure") == 0) {
            /*
             * Reconfigure pending package.
             */
            rv = xbps_configure_pkg(xhp, pkgname, false, false, false);
            if (rv != 0)
                goto out;
        } else {
            /*
             * Install or update a package.
             */
            if (strcmp(tract, "update") == 0)
                update = true;
            else
                install = true;

            if (update) {
                /*
                 * Update a package: execute pre-remove
                 * action if found before unpacking.
                 */
                xbps_set_cb_state(xhp, XBPS_STATE_UPDATE, 0,
                                  pkgname, version, NULL);
                rv = xbps_remove_pkg(xhp, pkgname, version,
                                     true, false);
                if (rv != 0) {
                    xbps_set_cb_state(xhp,
                                      XBPS_STATE_UPDATE_FAIL,
                                      rv, pkgname, version,
                                      "%s: [trans] failed to update "
                                      "package to `%s': %s", pkgver,
                                      version, strerror(rv));
                    goto out;
                }
            } else {
                /* Install a package */
                xbps_set_cb_state(xhp, XBPS_STATE_INSTALL,
                                  0, pkgname, version, NULL);
            }
            /*
             * Unpack binary package.
             */
            if ((rv = xbps_unpack_binary_pkg(xhp, obj)) != 0)
                goto out;
            /*
             * Register package.
             */
            if ((rv = xbps_register_pkg(xhp, obj, false)) != 0)
                goto out;
        }
    }
    prop_object_iterator_reset(iter);

    /* force a flush now packages were removed/unpacked */
    if ((rv = xbps_pkgdb_update(xhp, true)) != 0)
        goto out;

    /* if there are no packages to install or update we are done */
    if (!update && !install)
        goto out;
    /*
     * Configure all unpacked packages.
     */
    xbps_set_cb_state(xhp, XBPS_STATE_TRANS_CONFIGURE, 0, NULL, NULL, NULL);

    i = 0;
    while ((obj = prop_object_iterator_next(iter)) != NULL) {
        if (xhp->transaction_frequency_flush > 0 &&
                ++i >= xhp->transaction_frequency_flush) {
            if ((rv = xbps_pkgdb_update(xhp, true)) != 0)
                goto out;

            i = 0;
        }

        prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
        if ((strcmp(tract, "remove") == 0) ||
                (strcmp(tract, "configure") == 0))
            continue;

        prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
        prop_dictionary_get_cstring_nocopy(obj, "version", &version);
        update = false;
        if (strcmp(tract, "update") == 0)
            update = true;

        rv = xbps_configure_pkg(xhp, pkgname, false, update, false);
        if (rv != 0)
            goto out;
        /*
         * Notify client callback when a package has been
         * installed or updated.
         */
        if (update) {
            xbps_set_cb_state(xhp, XBPS_STATE_UPDATE_DONE, 0,
                              pkgname, version, NULL);
        } else {
            xbps_set_cb_state(xhp, XBPS_STATE_INSTALL_DONE, 0,
                              pkgname, version, NULL);
        }
    }

    /* Force a flush now that packages are configured */
    rv = xbps_pkgdb_update(xhp, true);
out:
    prop_object_iterator_release(iter);

    return rv;
}
Example #23
0
static void
btsco_attach(device_t parent, device_t self, void *aux)
{
	struct btsco_softc *sc = device_private(self);
	prop_dictionary_t dict = aux;
	prop_object_t obj;

	/*
	 * Init softc
	 */
	sc->sc_vgs = 200;
	sc->sc_vgm = 200;
	sc->sc_state = BTSCO_CLOSED;
	sc->sc_name = device_xname(self);
	cv_init(&sc->sc_connect, "connect");
	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);

	/*
	 * copy in our configuration info
	 */
	obj = prop_dictionary_get(dict, BTDEVladdr);
	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));

	obj = prop_dictionary_get(dict, BTDEVraddr);
	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));

	obj = prop_dictionary_get(dict, BTDEVservice);
	if (prop_string_equals_cstring(obj, "HF")) {
		sc->sc_flags |= BTSCO_LISTEN;
		aprint_verbose(" listen mode");
	}

	obj = prop_dictionary_get(dict, BTSCOchannel);
	if (prop_object_type(obj) != PROP_TYPE_NUMBER
	    || prop_number_integer_value(obj) < RFCOMM_CHANNEL_MIN
	    || prop_number_integer_value(obj) > RFCOMM_CHANNEL_MAX) {
		aprint_error(" invalid %s", BTSCOchannel);
		return;
	}
	sc->sc_channel = prop_number_integer_value(obj);

	aprint_verbose(" channel %d", sc->sc_channel);
	aprint_normal("\n");

	DPRINTF("sc=%p\n", sc);

	/*
	 * set up transmit interrupt
	 */
	sc->sc_intr = softint_establish(SOFTINT_NET, btsco_intr, sc);
	if (sc->sc_intr == NULL) {
		aprint_error_dev(self, "softint_establish failed\n");
		return;
	}

	/*
	 * attach audio device
	 */
	sc->sc_audio = audio_attach_mi(&btsco_if, sc, self);
	if (sc->sc_audio == NULL) {
		aprint_error_dev(self, "audio_attach_mi failed\n");
		return;
	}

	pmf_device_register(self, NULL, NULL);
}
Example #24
0
int
quota_handle_cmd(struct mount *mp, struct lwp *l, prop_dictionary_t cmddict)
{
	int error = 0;
	const char *cmd, *type;
	prop_array_t datas;
	int q2type;

	if (!prop_dictionary_get_cstring_nocopy(cmddict, "command", &cmd))
		return EINVAL;
	if (!prop_dictionary_get_cstring_nocopy(cmddict, "type", &type))
		return EINVAL;
	if (!strcmp(type, QUOTADICT_CLASS_USER)) {
		q2type = USRQUOTA;
	} else if (!strcmp(type, QUOTADICT_CLASS_GROUP)) {
		q2type = GRPQUOTA;
	} else
		return EOPNOTSUPP;
	datas = prop_dictionary_get(cmddict, "data");
	if (datas == NULL || prop_object_type(datas) != PROP_TYPE_ARRAY)
		return EINVAL;

	prop_object_retain(datas);
	prop_dictionary_remove(cmddict, "data"); /* prepare for return */

	if (strcmp(cmd, "get version") == 0) {
		error = quota_handle_cmd_get_version(mp, l, cmddict, datas);
		goto end;
	}
	if (strcmp(cmd, "quotaon") == 0) {
		error = quota_handle_cmd_quotaon(mp, l, cmddict,
		    q2type, datas);
		goto end;
	}
	if (strcmp(cmd, "quotaoff") == 0) {
		error = quota_handle_cmd_quotaoff(mp, l, cmddict,
		    q2type, datas);
		goto end;
	}
	if (strcmp(cmd, "get") == 0) {
		error = quota_handle_cmd_get(mp, l, cmddict, q2type, datas);
		goto end;
	}
	if (strcmp(cmd, "set") == 0) {
		error = quota_handle_cmd_set(mp, l, cmddict, q2type, datas);
		goto end;
	}
	if (strcmp(cmd, "getall") == 0) {
		error = quota_handle_cmd_getall(mp, l, cmddict, q2type, datas);
		goto end;
	}
	if (strcmp(cmd, "clear") == 0) {
		error = quota_handle_cmd_clear(mp, l, cmddict, q2type, datas);
		goto end;
	}
	error = EOPNOTSUPP;
end:
	error = (prop_dictionary_set_int8(cmddict, "return",
	    error) ? 0 : ENOMEM);
	prop_object_release(datas);
	return error;
}
Example #25
0
static int __noinline
npf_mk_tables(npf_tableset_t *tblset, prop_array_t tables,
    prop_dictionary_t errdict)
{
	prop_object_iterator_t it;
	prop_dictionary_t tbldict;
	int error = 0;

	/* Tables - array. */
	if (prop_object_type(tables) != PROP_TYPE_ARRAY) {
		NPF_ERR_DEBUG(errdict);
		return EINVAL;
	}

	it = prop_array_iterator(tables);
	while ((tbldict = prop_object_iterator_next(it)) != NULL) {
		const char *name;
		npf_table_t *t;
		u_int tid;
		int type;

		/* Table - dictionary. */
		if (prop_object_type(tbldict) != PROP_TYPE_DICTIONARY) {
			NPF_ERR_DEBUG(errdict);
			error = EINVAL;
			break;
		}

		/* Table name, ID and type.  Validate them. */
		if (!prop_dictionary_get_cstring_nocopy(tbldict, "name", &name)) {
			NPF_ERR_DEBUG(errdict);
			error = EINVAL;
			break;
		}
		prop_dictionary_get_uint32(tbldict, "id", &tid);
		prop_dictionary_get_int32(tbldict, "type", &type);
		error = npf_table_check(tblset, name, tid, type);
		if (error) {
			NPF_ERR_DEBUG(errdict);
			break;
		}

		/* Get the entries or binary data. */
		prop_array_t ents = prop_dictionary_get(tbldict, "entries");
		prop_object_t obj = prop_dictionary_get(tbldict, "data");
		void *blob = prop_data_data(obj);
		size_t size = prop_data_size(obj);

		if (type == NPF_TABLE_CDB && (blob == NULL || size == 0)) {
			NPF_ERR_DEBUG(errdict);
			error = EINVAL;
			break;
		}
		if (type == NPF_TABLE_HASH) {
			size = 1024; /* XXX */
		}

		/* Create and insert the table. */
		t = npf_table_create(name, tid, type, blob, size);
		if (t == NULL) {
			NPF_ERR_DEBUG(errdict);
			error = ENOMEM;
			break;
		}
		error = npf_tableset_insert(tblset, t);
		KASSERT(error == 0);

		if (ents && (error = npf_mk_table_entries(t, ents)) != 0) {
			NPF_ERR_DEBUG(errdict);
			break;
		}
	}
	prop_object_iterator_release(it);
	/*
	 * Note: in a case of error, caller will free the tableset.
	 */
	return error;
}
static int
bthub_pioctl(dev_t devno, unsigned long cmd, prop_dictionary_t dict,
    int flag, struct lwp *l)
{
	prop_data_t laddr, raddr;
	prop_string_t service;
	prop_dictionary_t prop;
	prop_object_t obj;
	device_t dev, self;
	deviter_t di;
	int unit;

	/* validate local address */
	laddr = prop_dictionary_get(dict, BTDEVladdr);
	if (prop_data_size(laddr) != sizeof(bdaddr_t))
		return EINVAL;

	/* locate the relevant bthub */
	for (unit = 0 ; ; unit++) {
		if (unit == bthub_cd.cd_ndevs)
			return ENXIO;

		self = device_lookup(&bthub_cd, unit);
		if (self == NULL)
			continue;

		prop = device_properties(self);
		obj = prop_dictionary_get(prop, BTDEVladdr);
		if (prop_data_equals(laddr, obj))
			break;
	}

	/* validate remote address */
	raddr = prop_dictionary_get(dict, BTDEVraddr);
	if (prop_data_size(raddr) != sizeof(bdaddr_t)
	    || bdaddr_any(prop_data_data_nocopy(raddr)))
		return EINVAL;

	/* validate service name */
	service = prop_dictionary_get(dict, BTDEVservice);
	if (prop_object_type(service) != PROP_TYPE_STRING)
		return EINVAL;

	/* locate matching child device, if any */
	deviter_init(&di, 0);
	while ((dev = deviter_next(&di)) != NULL) {
		if (device_parent(dev) != self)
			continue;

		prop = device_properties(dev);

		obj = prop_dictionary_get(prop, BTDEVraddr);
		if (!prop_object_equals(raddr, obj))
			continue;

		obj = prop_dictionary_get(prop, BTDEVservice);
		if (!prop_object_equals(service, obj))
			continue;

		break;
	}
	deviter_release(&di);

	switch (cmd) {
	case BTDEV_ATTACH:	/* attach BTDEV */
		if (dev != NULL)
			return EADDRINUSE;

		dev = config_found(self, dict, bthub_print);
		if (dev == NULL)
			return ENXIO;

		prop = device_properties(dev);
		prop_dictionary_set(prop, BTDEVladdr, laddr);
		prop_dictionary_set(prop, BTDEVraddr, raddr);
		prop_dictionary_set(prop, BTDEVservice, service);
		break;

	case BTDEV_DETACH:	/* detach BTDEV */
		if (dev == NULL)
			return ENXIO;

		config_detach(dev, DETACH_FORCE);
		break;
	}

	return 0;
}
Example #27
0
/*
 * ae_attach:
 *
 *	Attach an ae interface to the system.
 */
void
ae_attach(device_t parent, device_t self, void *aux)
{
	const uint8_t *enaddr;
	prop_data_t ea;
	struct ae_softc *sc = device_private(self);
	struct arbus_attach_args *aa = aux;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	int i, error;

	sc->sc_dev = self;

	callout_init(&sc->sc_tick_callout, 0);

	printf(": Atheros AR531X 10/100 Ethernet\n");

	/*
	 * Try to get MAC address.
	 */
	ea = prop_dictionary_get(device_properties(sc->sc_dev), "mac-address");
	if (ea == NULL) {
		printf("%s: unable to get mac-addr property\n",
		    device_xname(sc->sc_dev));
		return;
	}
	KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
	KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
	enaddr = prop_data_data_nocopy(ea);

	/* Announce ourselves. */
	printf("%s: Ethernet address %s\n", device_xname(sc->sc_dev),
	    ether_sprintf(enaddr));

	sc->sc_cirq = aa->aa_cirq;
	sc->sc_mirq = aa->aa_mirq;
	sc->sc_st = aa->aa_bst;
	sc->sc_dmat = aa->aa_dmat;

	SIMPLEQ_INIT(&sc->sc_txfreeq);
	SIMPLEQ_INIT(&sc->sc_txdirtyq);

	/*
	 * Map registers.
	 */
	sc->sc_size = aa->aa_size;
	if ((error = bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
	    &sc->sc_sh)) != 0) {
		printf("%s: unable to map registers, error = %d\n",
		    device_xname(sc->sc_dev), error);
		goto fail_0;
	}

	/*
	 * Allocate the control data structures, and create and load the
	 * DMA map for it.
	 */
	if ((error = bus_dmamem_alloc(sc->sc_dmat,
	    sizeof(struct ae_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
	    1, &sc->sc_cdnseg, 0)) != 0) {
		printf("%s: unable to allocate control data, error = %d\n",
		    device_xname(sc->sc_dev), error);
		goto fail_1;
	}

	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
	    sizeof(struct ae_control_data), (void **)&sc->sc_control_data,
	    BUS_DMA_COHERENT)) != 0) {
		printf("%s: unable to map control data, error = %d\n",
		    device_xname(sc->sc_dev), error);
		goto fail_2;
	}

	if ((error = bus_dmamap_create(sc->sc_dmat,
	    sizeof(struct ae_control_data), 1,
	    sizeof(struct ae_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
		printf("%s: unable to create control data DMA map, "
		    "error = %d\n", device_xname(sc->sc_dev), error);
		goto fail_3;
	}

	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
	    sc->sc_control_data, sizeof(struct ae_control_data), NULL,
	    0)) != 0) {
		printf("%s: unable to load control data DMA map, error = %d\n",
		    device_xname(sc->sc_dev), error);
		goto fail_4;
	}

	/*
	 * Create the transmit buffer DMA maps.
	 */
	for (i = 0; i < AE_TXQUEUELEN; i++) {
		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
		    AE_NTXSEGS, MCLBYTES, 0, 0,
		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
			printf("%s: unable to create tx DMA map %d, "
			    "error = %d\n", device_xname(sc->sc_dev), i, error);
			goto fail_5;
		}
	}

	/*
	 * Create the receive buffer DMA maps.
	 */
	for (i = 0; i < AE_NRXDESC; i++) {
		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
			printf("%s: unable to create rx DMA map %d, "
			    "error = %d\n", device_xname(sc->sc_dev), i, error);
			goto fail_6;
		}
		sc->sc_rxsoft[i].rxs_mbuf = NULL;
	}

	/*
	 * Reset the chip to a known state.
	 */
	ae_reset(sc);

	/*
	 * From this point forward, the attachment cannot fail.  A failure
	 * before this point releases all resources that may have been
	 * allocated.
	 */
	sc->sc_flags |= AE_ATTACHED;

	/*
	 * Initialize our media structures.  This may probe the MII, if
	 * present.
	 */
	sc->sc_mii.mii_ifp = ifp;
	sc->sc_mii.mii_readreg = ae_mii_readreg;
	sc->sc_mii.mii_writereg = ae_mii_writereg;
	sc->sc_mii.mii_statchg = ae_mii_statchg;
	sc->sc_ethercom.ec_mii = &sc->sc_mii;
	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
	    ether_mediastatus);
	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
	    MII_OFFSET_ANY, 0);

	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
	} else
		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);

	sc->sc_tick = ae_mii_tick;

	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
	ifp->if_softc = sc;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	sc->sc_if_flags = ifp->if_flags;
	ifp->if_ioctl = ae_ioctl;
	ifp->if_start = ae_start;
	ifp->if_watchdog = ae_watchdog;
	ifp->if_init = ae_init;
	ifp->if_stop = ae_stop;
	IFQ_SET_READY(&ifp->if_snd);

	/*
	 * We can support 802.1Q VLAN-sized frames.
	 */
	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;

	/*
	 * Attach the interface.
	 */
	if_attach(ifp);
	ether_ifattach(ifp, enaddr);
	ether_set_ifflags_cb(&sc->sc_ethercom, ae_ifflags_cb);

	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
	    RND_TYPE_NET, RND_FLAG_DEFAULT);

	/*
	 * Make sure the interface is shutdown during reboot.
	 */
	sc->sc_sdhook = shutdownhook_establish(ae_shutdown, sc);
	if (sc->sc_sdhook == NULL)
		printf("%s: WARNING: unable to establish shutdown hook\n",
		    device_xname(sc->sc_dev));

	/*
	 * Add a suspend hook to make sure we come back up after a
	 * resume.
	 */
	sc->sc_powerhook = powerhook_establish(device_xname(sc->sc_dev),
	    ae_power, sc);
	if (sc->sc_powerhook == NULL)
		printf("%s: WARNING: unable to establish power hook\n",
		    device_xname(sc->sc_dev));
	return;

	/*
	 * Free any resources we've allocated during the failed attach
	 * attempt.  Do this in reverse order and fall through.
	 */
 fail_6:
	for (i = 0; i < AE_NRXDESC; i++) {
		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
			bus_dmamap_destroy(sc->sc_dmat,
			    sc->sc_rxsoft[i].rxs_dmamap);
	}
 fail_5:
	for (i = 0; i < AE_TXQUEUELEN; i++) {
		if (sc->sc_txsoft[i].txs_dmamap != NULL)
			bus_dmamap_destroy(sc->sc_dmat,
			    sc->sc_txsoft[i].txs_dmamap);
	}
	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
 fail_4:
	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
 fail_3:
	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
	    sizeof(struct ae_control_data));
 fail_2:
	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
 fail_1:
	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_size);
 fail_0:
	return;
}
Example #28
0
void
smsc_attach(device_t parent, device_t self, void *aux)
{
    struct smsc_softc *sc = device_private(self);
    struct usb_attach_arg *uaa = aux;
    usbd_device_handle dev = uaa->device;
    usb_interface_descriptor_t *id;
    usb_endpoint_descriptor_t *ed;
    char *devinfop;
    struct mii_data *mii;
    struct ifnet *ifp;
    int err, s, i;
    uint32_t mac_h, mac_l;

    sc->sc_dev = self;
    sc->sc_udev = dev;

    aprint_naive("\n");
    aprint_normal("\n");

    devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    aprint_normal_dev(self, "%s\n", devinfop);
    usbd_devinfo_free(devinfop);

    err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
    if (err) {
        aprint_error_dev(self, "failed to set configuration"
                         ", err=%s\n", usbd_errstr(err));
        return;
    }
    /* Setup the endpoints for the SMSC LAN95xx device(s) */
    usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 0);
    usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 0);
    mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);

    err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
    if (err) {
        aprint_error_dev(self, "getting interface handle failed\n");
        return;
    }

    id = usbd_get_interface_descriptor(sc->sc_iface);

    if (sc->sc_udev->speed >= USB_SPEED_HIGH)
        sc->sc_bufsz = SMSC_MAX_BUFSZ;
    else
        sc->sc_bufsz = SMSC_MIN_BUFSZ;

    /* Find endpoints. */
    for (i = 0; i < id->bNumEndpoints; i++) {
        ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
        if (!ed) {
            aprint_error_dev(self, "couldn't get ep %d\n", i);
            return;
        }
        if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
                UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
            sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
        } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
                   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
            sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
        } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
                   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
            sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
        }
    }

    s = splnet();

    ifp = &sc->sc_ec.ec_if;
    ifp->if_softc = sc;
    strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    ifp->if_init = smsc_init;
    ifp->if_ioctl = smsc_ioctl;
    ifp->if_start = smsc_start;
    ifp->if_stop = smsc_stop;

#ifdef notyet
    /*
     * We can do TCPv4, and UDPv4 checksums in hardware.
     */
    ifp->if_capabilities |=
        /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
        /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
#endif

    sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;

    /* Setup some of the basics */
    sc->sc_phyno = 1;

    /*
     * Attempt to get the mac address, if an EEPROM is not attached this
     * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
     * address based on urandom.
     */
    memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);

    prop_dictionary_t dict = device_properties(self);
    prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");

    if (eaprop != NULL) {
        KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
        KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
        memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
               ETHER_ADDR_LEN);
    } else
        /* Check if there is already a MAC address in the register */
        if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
                (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
            sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
            sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
            sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
            sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
            sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
            sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
        }
Example #29
0
static void
bthidev_attach(device_t parent, device_t self, void *aux)
{
	struct bthidev_softc *sc = device_private(self);
	prop_dictionary_t dict = aux;
	prop_object_t obj;
	device_t dev;
	struct bthidev_attach_args bha;
	struct bthidev *hidev;
	struct hid_data *d;
	struct hid_item h;
	const void *desc;
	int locs[BTHIDBUSCF_NLOCS];
	int maxid, rep, dlen;

	/*
	 * Init softc
	 */
	sc->sc_dev = self;
	LIST_INIT(&sc->sc_list);
	callout_init(&sc->sc_reconnect, 0);
	callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
	sc->sc_state = BTHID_CLOSED;
	sc->sc_flags = BTHID_CONNECTING;
	sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
	sc->sc_intpsm = L2CAP_PSM_HID_INTR;

	sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);

	/*
	 * extract config from proplist
	 */
	obj = prop_dictionary_get(dict, BTDEVladdr);
	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));

	obj = prop_dictionary_get(dict, BTDEVraddr);
	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));

	obj = prop_dictionary_get(dict, BTDEVmode);
	if (prop_object_type(obj) == PROP_TYPE_STRING) {
		if (prop_string_equals_cstring(obj, BTDEVauth))
			sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
		else if (prop_string_equals_cstring(obj, BTDEVencrypt))
			sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
		else if (prop_string_equals_cstring(obj, BTDEVsecure))
			sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
		else  {
			aprint_error(" unknown %s\n", BTDEVmode);
			return;
		}

		aprint_verbose(" %s %s", BTDEVmode,
					 prop_string_cstring_nocopy(obj));
	}

	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
		sc->sc_ctlpsm = prop_number_integer_value(obj);
		if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
			aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
			return;
		}
	}

	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
	if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
		sc->sc_intpsm = prop_number_integer_value(obj);
		if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
			aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
			return;
		}
	}

	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
	if (prop_object_type(obj) == PROP_TYPE_DATA) {
		dlen = prop_data_size(obj);
		desc = prop_data_data_nocopy(obj);
	} else {
		aprint_error(" no %s\n", BTHIDEVdescriptor);
		return;
	}

	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
	if (prop_object_type(obj) == PROP_TYPE_BOOL
	    && !prop_bool_true(obj))
		sc->sc_flags |= BTHID_RECONNECT;

	/*
	 * Parse the descriptor and attach child devices, one per report.
	 */
	maxid = -1;
	h.report_ID = 0;
	d = hid_start_parse(desc, dlen, hid_none);
	while (hid_get_item(d, &h)) {
		if (h.report_ID > maxid)
			maxid = h.report_ID;
	}
	hid_end_parse(d);

	if (maxid < 0) {
		aprint_error(" no reports found\n");
		return;
	}

	aprint_normal("\n");

	for (rep = 0 ; rep <= maxid ; rep++) {
		if (hid_report_size(desc, dlen, hid_feature, rep) == 0
		    && hid_report_size(desc, dlen, hid_input, rep) == 0
		    && hid_report_size(desc, dlen, hid_output, rep) == 0)
			continue;

		bha.ba_desc = desc;
		bha.ba_dlen = dlen;
		bha.ba_input = bthidev_null;
		bha.ba_feature = bthidev_null;
		bha.ba_output = bthidev_output;
		bha.ba_id = rep;

		locs[BTHIDBUSCF_REPORTID] = rep;

		dev = config_found_sm_loc(self, "bthidbus",
					locs, &bha, bthidev_print, config_stdsubmatch);
		if (dev != NULL) {
			hidev = device_private(dev);
			hidev->sc_dev = dev;
			hidev->sc_parent = self;
			hidev->sc_id = rep;
			hidev->sc_input = bha.ba_input;
			hidev->sc_feature = bha.ba_feature;
			LIST_INSERT_HEAD(&sc->sc_list, hidev, sc_next);
		}
	}

	/*
	 * start bluetooth connections
	 */
	mutex_enter(bt_lock);
	if ((sc->sc_flags & BTHID_RECONNECT) == 0)
		bthidev_listen(sc);

	if (sc->sc_flags & BTHID_CONNECTING)
		bthidev_connect(sc);
	mutex_exit(bt_lock);
}
Example #30
0
static
int
swsensor_init(void *arg)
{
	int error, val = 0;
	const char *key, *str;
	prop_dictionary_t pd = (prop_dictionary_t)arg;
	prop_object_t po, obj;
	prop_object_iterator_t iter;
	prop_type_t type;
	const struct sme_descr_entry *descr;

	swsensor_sme = sysmon_envsys_create();
	if (swsensor_sme == NULL)
		return ENOTTY;

	swsensor_sme->sme_name = "swsensor";
	swsensor_sme->sme_cookie = &swsensor_edata;
	swsensor_sme->sme_refresh = swsensor_refresh;
	swsensor_sme->sme_set_limits = NULL;
	swsensor_sme->sme_get_limits = NULL;

	/* Set defaults in case no prop dictionary given */

	swsensor_edata.units = ENVSYS_INTEGER;
	swsensor_edata.flags = 0;
	sw_sensor_mode = 0;
	sw_sensor_value = 0;
	sw_sensor_limit = 0;

	/* Iterate over the provided dictionary, if any */
	if (pd != NULL) {
		iter = prop_dictionary_iterator(pd);
		if (iter == NULL)
			return ENOMEM;

		while ((obj = prop_object_iterator_next(iter)) != NULL) {
			key = prop_dictionary_keysym_cstring_nocopy(obj);
			po  = prop_dictionary_get_keysym(pd, obj);
			type = prop_object_type(po);
			if (type == PROP_TYPE_NUMBER)
				val = prop_number_integer_value(po);

			/* Sensor type/units */
			if (strcmp(key, "type") == 0) {
				if (type == PROP_TYPE_NUMBER) {
					descr = sme_find_table_entry(
							SME_DESC_UNITS, val);
					if (descr == NULL)
						return EINVAL;
					swsensor_edata.units = descr->type;
					continue;
				}
				if (type != PROP_TYPE_STRING)
					return EINVAL;
				str = prop_string_cstring_nocopy(po);
				descr = sme_find_table_desc(SME_DESC_UNITS,
							    str);
				if (descr == NULL)
					return EINVAL;
				swsensor_edata.units = descr->type;
				continue;
			}

			/* Sensor flags */
			if (strcmp(key, "flags") == 0) {
				if (type != PROP_TYPE_NUMBER)
					return EINVAL;
				swsensor_edata.flags = val;
				continue;
			}

			/* Sensor limit behavior
			 *	0 - simple sensor, no hw limits
			 *	1 - simple sensor, hw provides initial limit
			 *	2 - complex sensor, hw provides settable 
			 *	    limits and does its own limit checking
			 */
			if (strcmp(key, "mode") == 0) {
				if (type != PROP_TYPE_NUMBER)
					return EINVAL;
				sw_sensor_mode = val;
				if (sw_sensor_mode > 2)
					sw_sensor_mode = 2;
				else if (sw_sensor_mode < 0)
					sw_sensor_mode = 0;
				continue;
			}

			/* Grab any limit that might be specified */
			if (strcmp(key, "limit") == 0) {
				if (type != PROP_TYPE_NUMBER)
					return EINVAL;
				sw_sensor_limit = val;
				continue;
			}

			/* Grab the initial value */
			if (strcmp(key, "value") == 0) {
				if (type != PROP_TYPE_NUMBER)
					return EINVAL;
				sw_sensor_value = val;
				continue;
			}

			/* Grab value_min and value_max */
			if (strcmp(key, "value_min") == 0) {
				if (type != PROP_TYPE_NUMBER)
					return EINVAL;
				swsensor_edata.value_min = val;
				swsensor_edata.flags |= ENVSYS_FVALID_MIN;
				continue;
			}
			if (strcmp(key, "value_max") == 0) {
				if (type != PROP_TYPE_NUMBER)
					return EINVAL;
				swsensor_edata.value_max = val;
				swsensor_edata.flags |= ENVSYS_FVALID_MAX;
				continue;
			}

			/* See if sensor reports percentages vs raw values */
			if (strcmp(key, "percentage") == 0) {
				if (type != PROP_TYPE_BOOL)
					return EINVAL;
				if (prop_bool_true(po))
					swsensor_edata.flags |= ENVSYS_FPERCENT;
				continue;
			}

			/* Unrecognized dicttionary object */
#ifdef DEBUG
			printf("%s: unknown attribute %s\n", __func__, key);
#endif
			return EINVAL;

		} /* while */
		prop_object_iterator_release(iter);
	}

	/* Initialize limit processing */
	if (sw_sensor_mode >= 1)
		swsensor_sme->sme_get_limits = swsensor_get_limits;

	if (sw_sensor_mode == 2)
		swsensor_sme->sme_set_limits = swsensor_set_limits;

	if (sw_sensor_mode != 0) {
		swsensor_edata.flags |= ENVSYS_FMONLIMITS;
		swsensor_get_limits(swsensor_sme, &swsensor_edata,
		    &sw_sensor_deflims, &sw_sensor_defprops);
	}

	strlcpy(swsensor_edata.desc, "sensor", ENVSYS_DESCLEN);

	/* Wait for refresh to validate the sensor value */
	swsensor_edata.state = ENVSYS_SINVALID;
	sw_sensor_state = ENVSYS_SVALID;

	error = sysmon_envsys_sensor_attach(swsensor_sme, &swsensor_edata);
	if (error != 0) {
		aprint_error("sysmon_envsys_sensor_attach failed: %d\n", error);
		return error;
	}

	error = sysmon_envsys_register(swsensor_sme);
	if (error != 0) {
		aprint_error("sysmon_envsys_register failed: %d\n", error);
		return error;
	}

	sysctl_swsensor_setup();
	aprint_normal("swsensor: initialized\n");

	return 0;
}